2

I have a problem. Since hours I try to get a upload script working. I also read some other questions on stackoverflow but nothing helped. The webspace allows file_upload and the size of the file is not too large. The directory where to move the downloads exists, too. My html code is:

Upload: <input type="file" name="file">

And my PHP code is:

if ($_FILES['file']['error'] == 0) {
    $tmp_name = $_FILES["file"]["tmp_name"];
    $name_file = $_FILES["file"]["name"];
    move_uploaded_file($tmp_name, "upload/" . $name_file);
}

I echoed $_FILES and the file was in there and there was no error. Everytime I try to upload the file (I also tried to upload other files and file types) move_uploaded_file returned false.

EDIT: The folder is writeable now.

bw2801
  • 321
  • 2
  • 8
  • 15
  • 2
    Are you sure that `upload/` exists and is writeable? – BenM Mar 30 '13 at 18:48
  • Yes the folder exists and the permissions are 755. I think that these are the right permissions. – bw2801 Mar 30 '13 at 18:49
  • 2
    you can add this before move_uploaded_file(): `if(!is_writable("upload") || !is_dir("upload")){ echo "error in dir"; }` –  Mar 30 '13 at 18:52
  • if you check [here](http://php.net/manual/en/function.move-uploaded-file.php) you will read that **If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE**. Just check your uploading folder permissions. – alexcasalboni Mar 30 '13 at 18:52
  • Double check that it is writable by running `is_writable()` http://www.php.net/manual/en/function.is-writable.php – Luke Shaheen Mar 30 '13 at 18:53
  • `is_writable()` return false and `is_dir()` returns true. The dir-permissions are 755. Is this wrong? – bw2801 Mar 30 '13 at 18:57
  • 1
    Yes, permissions should be 666. – BenM Mar 30 '13 at 18:58
  • Okay, I updated the permissions but the file does not get moved... – bw2801 Mar 30 '13 at 19:00
  • And what does `is_writable()` return? – BenM Mar 30 '13 at 19:01
  • Then I'm afraid that the problem must lie somewhere else... – BenM Mar 30 '13 at 19:04
  • Any ideas? I'm clueless... :( – bw2801 Mar 30 '13 at 19:17
  • What type of file are you uploading? Have you tried uploading to a different directory, or `move_uploaded_file($tmp_name, "upload/ahardcodedfilename.yourext);` where `yourext` is the extension of the file you are uploading? Just trying to help you identify the issue. – Luke Shaheen Mar 30 '13 at 20:01
  • I'm uploading .zip and .rar files. I'll try it. – bw2801 Mar 30 '13 at 22:32
  • Nope. Another directory does not work (it is writable) and a hardcoded filename does not work, too. – bw2801 Mar 30 '13 at 22:36

4 Answers4

4

Okay, I solved my problem. I enabled error_resporting() and because any reason the permissions weren't set. Now they are 777 and I can move the files.

bw2801
  • 321
  • 2
  • 8
  • 15
3

Folder upload will not writable by others by setting permission 755. You may set permission atleast 666 through chmod command or better set ownership for upload folder for apache user.

I setup it On Ubuntu like that

sudo chown www-data:www-data upload 
kuldeep.kamboj
  • 2,566
  • 3
  • 26
  • 63
2

Set the folder permissions on upload to 666. This should ensure that the directory is correctly writeable.

BenM
  • 52,573
  • 26
  • 113
  • 168
2

Hope this may help someone.

I met the same problem, my $_FILES['file']['tmp_name'] has proper returns while move_uploaded_file($_FILES['file']['tmp_name'],$destination_dir) returns nothing. So I ran this check:

if(!is_writable($destination_dir)){ echo "error in dir"; }

and got the message "error in dir". Even my $destination_dir in move_uploaded_file has been set to 777. After spending a long time searching, I found this answer. So these two commands solved my problem:

sudo chmod -R 777 Ad_images/
sudo chcon -R -t httpd_sys_rw_content_t Ad_images/

Now the is_writable($destination_dir) returns 1 and the uploading is successful.

TL;DR: Make sure your destination directory is writable for php.

Community
  • 1
  • 1
Demonedge
  • 1,363
  • 4
  • 18
  • 33