How can the move_uploaded_file() function be properly utilized in PHP image uploads?
When uploading images in PHP, the move_uploaded_file() function is used to move the uploaded file to a specified destination. To properly utilize this function, ensure that the destination directory is writeable by the web server and that the file is actually uploaded before attempting to move it. Additionally, always validate and sanitize user input to prevent security vulnerabilities.
<?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
-
strpos PHP common mistakes
-
MySQL sorting order PHP
-
PHP error log troubleshooting Internal Server Error 500