10

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??

eladyanai
  • 1,063
  • 2
  • 15
  • 34

7 Answers7

18

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
rjv
  • 6,058
  • 5
  • 27
  • 49
  • you were right, i have already solved it, well... not really me, i called to the hosting server tech. and told them my problem, apparently, they didn't give permissions to the PHP (www-data) or whatever... after they changed it, it was all good! – eladyanai Jan 02 '12 at 09:19
14

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • this is what i get: $_FILES['file']['error'] = 0 Warning: move_uploaded_file(/temp/754327953-129265952-content): failed to open stream: No such file or directory in C:\inetpub\wwwroot\LocalUser\notebook\server\UploadFile.php on line 16 Warning: move_uploaded_file(): Unable to move 'C:\Windows\Temp\phpCC2E.tmp' to '/temp/bla.txt' in C:\inetpub\wwwroot\LocalUser\notebook\server\UploadFile.php on line 16 – eladyanai Dec 10 '11 at 16:13
  • Look! it made everything clear! :) Feel the power of the error messagfe! It seems something wrong with the file destination. it is trying to open some strange file `/temp/754327953-129265952-content`. try to use another destination dir? – Your Common Sense Dec 10 '11 at 16:16
  • hmm... i guess so, cause i didn't get it... i edited a small part from the warning filename from 754327953-129265952-content to bla.txt, but i don't get the part of the failed to open stream: No such file or directory in. what am i supposed to do with that!? – eladyanai Dec 10 '11 at 16:19
  • is there a C:\temp\ directory on your disk? – Your Common Sense Dec 10 '11 at 16:22
  • the thing is that this is a remote server (on my university) so i have no idea if the directory exists or if there is a problem with it. the only thing i can do is upload a php file using ftp client and then use it... – eladyanai Dec 10 '11 at 16:28
4

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.

abriggs
  • 744
  • 7
  • 16
2

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.

Ted
  • 551
  • 1
  • 7
  • 16
2

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.

josegil
  • 365
  • 1
  • 8
1

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.

zyamys
  • 1,609
  • 1
  • 21
  • 23
0

Did you try to activate error_reporting?

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

  • thank you for you fast comment. this is what i see in the phpinfo: file_uploads=On, max_file_uploads=20, upload_max_filesize=2M, upload_tmp_dir=C:\Windows\Temp. about the error_reporting i have just find out about it by your post, so i'm working on it... btw- the file that i am uploading is small (10kb max) – eladyanai Dec 10 '11 at 16:01
  • since i am working on a remote server of my university i didn't have permissions to go to c:\Windows\Temp, and this was the value in the PHP INI file, i asked them to change it into a path that i have permissions to and it worked... thank you for your time! you and @Col. Shrapnel – eladyanai Dec 12 '11 at 15:39