• 5

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

PHP move_uploaded_file() error?

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" is another question. that can be answered. but you have to understand very basic rule: without error message you are blind, deaf and mute. Your only desire and efforts should be to get error message. Am I clear enough?
      • 2
    • A programmer should not trust. Not to a customer who said something, nor a volunteer from SO, nor himself. But only in error message a programmer can trust.
      • 2
    • Do you have the images folder on the web server? Is the folder read only, or can you write to is also?

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:

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

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

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

UPLOAD_ERR_NO_FILE = Value: 4; No file was uploaded.

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

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

UPLOAD_ERR_EXTENSION = 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.

  • 55
Reply Report

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

  • 39
Reply Report
    • I can upload files in this folder manually using FileZilla, it means it has the rights. Any other possibility ?
      • 1
    • FileZilla doesn't prove that the web server has permissions. The web server will most likely run under a different user than you are. To make sure it has permissions, just set the permissions to 777, then test. This should prove if it's a permission issue or something else.

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.

  • 37
Reply Report

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

  • 16
Reply Report
      • 2
    • Well, it's fine to show errors on a development site. It's not always possible to redirect errors to a log file (e.g. on shared hosting).

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

  • 6
Reply Report
    • The problem was solved with the keyword "source". Look at that: "move_uploaded_file() will return FALSE and issue a warning in the error log if the apache process does not have read/write permissions to 'source' or destination directories." So it is not enough to give all permission to your project directory and also should give all permission to the source file's location.

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.

  • 2
Reply Report
$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';
  • 0
Reply Report

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
  • 0
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions