Search code examples
phplinuxfile-uploadfile-permissionspermission-denied

PHP move_uploaded_file() FAILS without reason


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.


Solution

  • 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.