Description of the problem that appears:
There was a problem today when implementing a PHP script that uploads the avatar image file when the user registers, the PHP script has been identified earlier
There is no error in uploading files on browser side.
The uploaded file is legal.
The uploaded file is an image file.
A unique file name has been generated on the server side.
Code
The next thing we should do is move the files from the temporary location to the fixed location, so I wrote the following script:
Move the file from the temporary location to the fixed position @move_uploaded_file ($_files[$image _fieldname][' tmp_name '), $upload _filename) or Handle_error (" Error storing picture file "," Moving file Error ". "{$upload _filename}");
The Code handle_error () function is my own definition of the error handler, when the Move_uploaded_file function performs an error, it jumps to the error page, when I execute the above script, the script jumps to the error page, it is obviously an error, First I have to check if my function parameter has an error:
$_files[$image _fieldname][' tmp_name ') $upload _filename//is the file path of my own combination, guaranteed Yes
According to the PHP manual, I passed in the function of the above two parameters are guaranteed to have no problem, so what is the case, the page is not an error (in front of the function I used the PHP "@" operator, so the page will not error)
@ operator
Note: Use PHP's @ operator sparingly in your code,
The @ operator can mask all problems that may be invalid from user input, or an SQL query that contains an incorrect column, or even a non-canonical URL error can be avoided, the code can even not check by the user, themselves or with the resulting error, in short, the @ operator can block out the code error message, A popular website often uses @ because they don't crash or stop at all, but in this case, other scenarios that resolve the error are used.
Look for the error log file
I didn't realize that the @ operator was blocking the error message, and I wanted to find the Apache error log file to see if I was using a development kit like XAMPP when I was building the PHP development environment, so the Error_log file is not the same as most articles on the web. , and in the end I
(My host is Ubuntu)
/opt/lampp/logs
Path to find the Php_error_log file, of course, the path also contains Apache error_log files, in the Php_error_log file I saw the error problem: insufficient authority, I finally found the wrong place: we store the image of the directory for PHP users do not have permission to execute PHP script users and I write script code, create picture folder user is not the same user
In fact, we needn't be so troublesome, we just have to remove the @ operator in front of the function, then remove the error handler Handle_error () function, then we can see the error message on the Web page.
Modify target folder permissions
Anyway, we have found the source of the problem, this is a very happy thing, since the folder belongs to the user and permissions are not correct, then we just need to modify these issues:
Modify the user who pinned the picture to the folder and modify it to run Apache to execute the php script.
Change the permissions of a folder to 755
So who's the user running Apache, we use PHP scripts to get:
echo exec (' whoami '); Get the name of the user who executed the file, thereby modifying the permissions of the picture folder
So I got the script. The user is: daemon, what you get is probably not the same as mine.
Let's modify the folder's owning User:
Chown Daemon-r ~/web/hello_php/uploads
~/web/hello_php/uploads is the target path where I store the image, and-R represents a recursive change to the folder in the directory to which the user belongs.
Then Modify folder permissions
chmod 775-r ~/web/hello_php/uploads
So we're done, right?
Reference reading: http://www.manongjc.com/article/1494.html
Resolve PHP move_uploaded_file function move picture failed