eladyanai
eladyanai

Reputation: 1093

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_OK();
else
    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.

help??

Upvotes: 10

Views: 48764

Answers (7)

Your Common Sense
Your Common Sense

Reputation: 157838

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

error_reporting(E_ALL);

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

ini_set('display_errors',1);

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.

Upvotes: 14

Eddie
Eddie

Reputation: 580

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.

Upvotes: 2

rjv
rjv

Reputation: 6766

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

Upvotes: 18

zyamys
zyamys

Reputation: 1659

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.

Upvotes: 1

abriggs
abriggs

Reputation: 744

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.

Upvotes: 4

josegil
josegil

Reputation: 365

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.

Upvotes: 2

LordOfBerlin
LordOfBerlin

Reputation: 66

Did you try to activate error_reporting?

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

Upvotes: 0

Related Questions