PHP move_uploaded_file() FAILS without rea

2020-04-16 02:58发布

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.

3条回答
叛逆
2楼-- · 2020-04-16 03:30

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.

查看更多
3楼-- · 2020-04-16 03:36

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.

查看更多
Melony?
4楼-- · 2020-04-16 03:41

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.

查看更多
登录 后发表回答