Home » Php » file – PHP move_uploaded_file() error?

file – PHP move_uploaded_file() error?

Posted by: admin April 23, 2020 Leave a comment


I using following code and it is successfully uploading files on my local machine. It is showing “Successfully uploaded” on my local machine.

// Upload file
$moved = move_uploaded_file($_FILES["file"]["tmp_name"], "images/" . "myFile.txt" );

if( $moved ) {
  echo "Successfully uploaded";         
} else {
  echo "Not uploaded";

But when I used this code on my online server then it is not uploading file and just showing message “Not uploaded”.

How can I know that what is the problem and how can I get the actual problem to display to the user ?

How to&Answers:

Check that the web server has permissions to write to the “images/” directory


Edit the code to be as follows:

// Upload file
$moved = move_uploaded_file($_FILES["file"]["tmp_name"], "images/" . "myFile.txt" );

if( $moved ) {
  echo "Successfully uploaded";         
} else {
  echo "Not uploaded because of error #".$_FILES["file"]["error"];

It will give you one of the following error code values 1 to 8:

Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.

Value: 3; The uploaded file was only partially uploaded.

Value: 4; No file was uploaded.

Value: 6; Missing a temporary folder. Introduced in PHP 5.0.3.

Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.

Value: 8; A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help.


Try this:

$upload_dir = $_SERVER['DOCUMENT_ROOT'] . "/images/";

if (is_dir($upload_dir) && is_writable($upload_dir)) {
    // do upload logic here
} else {
    echo 'Upload directory is not writable, or does not exist.';

This will instantly flag any file permission errors.


How can I know that what is the problem

Easy. Refer to the error log of the webserver.

how can I get the actual problem to display to the user ?

NEVER do it.
An average user will unerstand nothing of this error.
A malicious user should get no feedback, especially in a form of very informative error message.

Just show a page with excuses.

If you don’t have access to the server’s error log, your task become more complicated.
There are several ways to get in touch with error messages.

To display error messages on screen you can add these lines to the code


or to make custom error logfile


and there are some other ways.
but you must understand that without actual error message you can’t move. It’s hard to be blind in the dark


move_uploaded_file() will return:

  1. FALSE if file name is invalid
  2. FALSE and issue a warning in the error log if the apache process does not have read/write permissions to source or destination directories

PHP Error Log

My php error log was at: /var/log/httpd/error_log and had these errors:

Warning: move_uploaded_file(images/robot.jpg): failed to open stream: Permission denied in /var/www/html/mysite/mohealth.php on line 78

Warning: move_uploaded_file(): Unable to move ‘/tmp/phpsKD2Qm’ to ‘images/robot.jpg’ in /var/www/html/mysite/mohealth.php on line 78

move_uploaded_file() tries to move files from a temporary directory to a destination directory. When apache process tried to move files, it could not read the temporary or write to the destination dir.

  1. Find which user is running Apache (Web Server)
    Check which user is running the apache service by this command: ps aux | grep httpd. The first column is the user name.

  2. Check Read Permission at Temporary Dir: Your can find the path to your temp dir by calling echo sys_get_tmp_dir(); in a php page. Then on the command line, issue ls -ld /tmp/temporary-dir to see if the apache user has access to read here

  3. Check Write Permission at Destination Dir: issue ls -ld /var/www/html/destination-directory to see if the apache user has access to write here

  4. Add permissions as necessary using chown or chgrp

  5. Restart Apache using sudo service httpd restart


Do you checks that file is uploaded ok ? Maybe you exceeded max_post_size, or max_upload_filesize. When login using FileZilla you are copying files as you, when uploading by PHP wiritng this file is from user that runs apache (for exaplme www-data), try to put chmod 755 for images.


or run suexec and never have to change permissions again.


Please check that your form tag have this attribute:



$uploadfile = $_SERVER['DOCUMENT_ROOT'].'/Thesis/images/';
$profic = uniqid(rand()).$_FILES["pic"]["name"]; 

    $moved = move_uploaded_file($_FILES["pic"]["tmp_name"], $uploadfile.$profic);
        echo "sucess";
        echo 'failed';


On virtual hosting check your disk quota.

if quota exceed, move_uploaded_file return error.

PS : I’ve been looking for this for a long time 🙂


Please check permission “images/” directory


I ran into a very obscure and annoying cause of error 6.
After goofing around with some NFS mounted volumes, uploads started failing.
Problem resolved by restarting services

systemctl restart php-fpm.service
systemctl restart httpd.service


In php.ini search for upload_max_filesize and post_max_size. I had the same problem and the solution was to change these values to a value greater than the file size.