What is the purpose of the move_uploaded_file() function in PHP?
The move_uploaded_file() function in PHP is used to move an uploaded file to a new location on the server. This function is commonly used in web applications that involve file uploads, such as uploading images or documents. By using this function, you can securely move the uploaded file to a specified directory on the server.
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
?>
Related Questions
- What is the potential issue with displaying a spinner in PHP during tasks like database import/export?
- What are the best practices for efficiently extracting tag names from HTML content using regular expressions in PHP?
- What are the advantages of using arrays in PHP to organize and display calendar data?