Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue with PHP file upload – "move_uploaded_files" not working as expected

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • 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!

>Solution :

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:

<?php
$targetDirectory = "uploads/";
$targetFile = $targetDirectory . basename($_FILES["fileToUpload"]["name"]);

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFile)) {
    echo "File uploaded successfully!";
} else {
    echo "Error uploading file.";
}
?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading