PHP move_uploaded_file() FAILS and i don't know why

this is my code:

$uploaddir = '/temp/';
$uploadfile = $uploaddir.basename($_FILES['file']['name']);

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
    send_error("ERROR - uploading file");

i have tried to upload with ftp_fput, ftp_put, move_uploaded_file, rename, copy and anything i can put my hands on. nothing seems to work.

i can’t understand what is the problem since move_uploaded_file returns only true or false and no error code.


Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Are you sure that the target directory has write permissions for world?ie,the third number in permission representation?
The files uploaded by php are owned by and comes under the group www-data

You can change the ownership by

[sudo] chown -R www-data folder // change owner
[sudo] chown -R www-data:www-data folder // change group and owner

Solution 2

i don’t know why

But you have to.

That’s what error messages are for.
Do you see any error message when something goes wrong? If not, then you have to check error logs.

Add this line at the top of your code


and this one, if it’s your local (not live) server


so you’ll be able to see errors onscreen

For the file uploads you have to check $_FILES['file']['error']) first. it it’s not 0, refer to the manual page for the actual message.

Solution 3

I experienced a similar problem when using move_uploaded_file which would fail to upload particular files with an $_FILES[‘filename’][‘error’] code of 0.

It turns out that the name of the file needs to be unique in relation to the destination directory. move_uploaded_file does not know how to handle identical files names.

Solution 4

Have you check the limit of the file size? One of the reason if crashing could be that you are trying to upload a file bigger than the limit in your configuration. Look at the config var “upload_max_filesize” in your php.ini and check the size of the file.

Solution 5

This caught me out too. Be aware of:

move_uploaded_file() is both safe mode and open_basedir aware. However, restrictions are placed only on the destination path as to allow the moving of uploaded files in which filename may conflict with such restrictions. move_uploaded_file() ensures the safety of this operation by allowing only those files uploaded through PHP to be moved.

These settings can cause the upload to fail if you try to move the file outside of your website base directory for example.

Solution 6

In addition to permissions, be sure to check that there is disk space available on your server. If not, move_uploaded_file() will fail with error 0.

Solution 7

Did you try to activate error_reporting?

You should check your php-config if file uploads are allowed.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply