I’m currently working on a file upload feature for my PHP application, and I’m encountering a problem with the move_uploaded_file function. Here’s a simplified version of my code:
<?php
$targetDirectory = "uploads/";
$targetFile = $targetDirectory . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_files($_FILES["fileToUpload"]["tmp_name"], $targetFile)) {
echo "File uploaded successfully!";
} else {
echo "Error uploading file.";
}
?>
Despite having the correct permissions set for the "uploads" directory, the file doesn’t seem to be moved there. I’ve checked the $_FILES array, and it contains the expected information. What could be causing this issue, and how can I troubleshoot and resolve it?
Additional Information:
- I’m using PHP 7.4.
- The "uploads" directory has the necessary write permissions.
- I’m testing this on a local development server.
Any help or insights would be greatly appreciated!
2
Answers
I have found another code
There is a small typo in your code. The function name should be move_uploaded_file (singular "file"), not move_uploaded_files (plural "files"). Here’s the corrected code: