2

I can't make php upload work. PHP 5.4.45 Centos 6.7. Apache 2.2.27.

I have a HTML file:

<form enctype="multipart/form-data" action="test2.php" method="POST">
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" name="submit"/>
</form>

And I have PHP file:

<?php
$uploaddir = '/home/michael/public_html/forum/files/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Error!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>

When I run it I get:

Error!
Here is some more debugging info:Array
(
[userfile] => Array
    (
        [name] => 184958.jpg
        [type] => image/jpeg
        [tmp_name] => /tmp/phpupab11
        [error] => 0
        [size] => 1473603
    )
)

And the file is not moved to new location. Instead I get a 0-size file with same name in new location.

-rw-r--r-- 1 michael michael 0 Apr  5 16:33 184958.jpg

Server log notes the error:

[error] [client xx.xx.xx.xx] PHP Warning:  move_uploaded_file(): 
Unable to move '/tmp/phpupab11' to '/home/michael/public_html/forum/files/184958.jpg'
in /home/michael/public_html/forum/test2.php on line 9

Seems the file is uploaded good to /tmp dir, but cannot be moved to another location. As much as I read it is a permission problem. But permission of folder "files" is seems good - 777 (with owner michael=username):

drwxrwxrwx  2 michael michael 1216512 Apr  5 13:16 files

As well PHP variables:

post_max_size   20M
upload_max_filesize   20M
upload_tmp_dir /tmp
file_uploads On
memory_limit 1024M

I would glad to get some help or at least direction to look for to resolve the issue. Thank you.

Shmuel
  • 488
  • 1
  • 6
  • 18
  • P.S. The same code was working on another server before I moved. – Shmuel Apr 05 '16 at 13:51
  • what about `forum` folder's permissions? – mitkosoft Apr 05 '16 at 13:55
  • Check if you have any warning in php. Activate all logs like here: http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display If you don't have any warning that mean the first paramether is not valid. Check here: http://php.net/manual/en/function.move-uploaded-file.php – Daniel Dudas Apr 05 '16 at 13:55
  • The fact that it created an empty file in that directory makes me think it isn't permissions related, is there free space on the partition? Try copying another file to /home/michael/public_html/forum/files/ as the web user (whatever PHP is running as) and see if you get any errors. – Devon Bessemer Apr 05 '16 at 13:57
  • PHP shows same error like server log. Warning: move_uploaded_file(): Unable to move '/tmp/phpjv2mgS' to '/home/michael/public_html/forum/files/153626.jpg' in /home/michael/public_html/forum/test2.php on line 12 – Shmuel Apr 05 '16 at 15:12
  • User michael can copy a file from /tmp to files/. As soon as there is no such file in the folder. – Shmuel Apr 05 '16 at 15:20
  • 1
    Two common issues: First, the webserver is not running as user michael. It is running as apache or www-user or something like that. Second, is this an selinux box? – kainaw Apr 05 '16 at 16:09
  • SELinux status: disabled Uploaded file in /tmp (what is created by php) is of user michael, so I assume PHP is running under this user. Whoami also returns michael – Shmuel Apr 05 '16 at 16:18
  • One more thing just found out. Everything working OK when I try to move the file to /somefolder on the root with normal permissions (777 and michael owner). But error when I try to do it to any folder in the /home dir and under that tree. Maybe because it owned by root. Can't change it. Any idea? Thank you. – Shmuel Apr 06 '16 at 13:21

3 Answers3

0

File owner must be the process the web server is running under, likely apache. Set the files directory to be owned by apache with write permissions.

Curt Evans
  • 346
  • 2
  • 4
  • Nothing changed. Tryed nobody:nobody, apache:apache, michael:michael... Same result. Only folders in root dir works. – Shmuel Apr 07 '16 at 07:55
0

The problem was not solved. No folder under /home was able to receive uploaded files. Neither permissions/ownership changes helped. Anyway, found workaround. Not ideal and not secure, but works and that's good enough until I'll find out the reason for that strange behavior. I created a folder in root dir (/files_of_forum) and the permissions (michael:michael 777) and changed in forum the dir of the attachments (what was original "files" to "../../../../files_of_forum". Moved all files from original folder to the new one. Now everything works. Thank all what tried to help.

Shmuel
  • 488
  • 1
  • 6
  • 18
0

For those who come here because of similar bugs, I found solution for my own SPECIAL unique case, I share this solution just in case you guys are unlucky like me (if any checklist below is a No answer for you please ignore this answer):

  1. You are in local development
  2. You have Avast installed
  3. Your WAMP server works fine but the server started by php artisan serve (Laravel) got problems for some reasons, you cannot run move_uploaded_file PHP method

Your Avast is blocking your php.exe from running zzzzzzzzz... Not because of permissions, directories exist or not, files size php.ini, save to public or storage, bla bla bla... Hope it helps those who are unlucky like me.

Phantom1412
  • 319
  • 4
  • 9