How can PHP be used to upload and rename files on a website?
To upload and rename files on a website using PHP, you can use the move_uploaded_file() function to move the uploaded file to a new location with a new name. You can also use the $_FILES superglobal array to access information about the uploaded file, such as its name and temporary location.
<?php
$uploadedFile = $_FILES['file']['tmp_name'];
$fileName = $_FILES['file']['name'];
$newFileName = 'new_file_name.txt';
if(move_uploaded_file($uploadedFile, 'uploads/' . $newFileName)){
echo "File uploaded and renamed successfully.";
} else{
echo "Error uploading file.";
}
?>
Related Questions
- How can developers securely log and view the SQL statements with variables sent to the database when using prepared statements in PHP?
- What are some alternative methods to using switch-case functions in PHP for sorting data based on different criteria?
- What are the best practices for displaying data on a website using PHP?