Home > php教程 > php手册 > Solving the problem of PHP move_uploaded_file function failing to move pictures

Solving the problem of PHP move_uploaded_file function failing to move pictures

WBOY
Release: 2016-10-08 16:04:42
Original
1157 people have browsed it

Description of the problem:
Today when implementing a PHP script that uploads avatar image files when users register, a problem occurred: The PHP script has been determined previously

There are no errors when uploading files on the browser side.
The uploaded files are legal.
The uploaded file is an image file.
Unique file names have been generated on the server side.
Code
The next thing we should do is move the file from the temporary location to the fixed location, so I wrote the following script:

//Move the file from the temporary location to the fixed location @move_uploaded_file($_FILES[$image_fieldname]['tmp_name'], $upload_filename) or handle_error("Error in storing image file", "Error in moving file" . "{$upload_filename }");
The code handle_error() function is an error handling function defined by myself. When the move_uploaded_file function executes an error, it will jump to the error page. When I execute the above script, the script jumps to the error page. Obviously something went wrong. First I checked whether there was an error in my function parameters:

$_FILES[$image_fieldname]['tmp_name']$upload_filename //It is the file path I combined myself, guaranteed to be correct
According to the PHP manual, the above two parameters I passed into the function are guaranteed to be no problem, so What's going on? There are no errors reported on the page (I used PHP's "@" operator in front of the function, so the page will not report errors)

@operator

Note: Be careful when using PHP's @ operator in your code. The
@ operator can screen out all problems that may come from invalid user input or an SQL query that contains an incorrect column or even a non-standard URL error. Avoided, the code may not even check for errors generated by the user, itself, or the system. In short, the @ operator can mask error messages from the code. A popular website often uses @ because they cannot crash or stop at all. But use other error-solving solutions in this situation.

Looking for the error log file
At that time, I didn’t realize that the @ operator blocked the error message. I wanted to look for the error log file of apache, because I used xampp when setting up the PHP development environment. development kit, so the error_log file is different from what most articles on the Internet say. In the end, I found it in
(my host is ubuntu)

The php_error_log file was found under the path

/opt/lampp/logs
. Of course, the error_log file of apache is also stored in this path. In the php_error_log file, I saw the error problem: insufficient permissions. I finally found the place where the error occurred: we stored The destination directory of the image does not have permissions for the user who executes PHP. The user who executes the PHP script is not the same user who wrote the script code and created the image folder

In fact, we don’t have to be so troublesome. We only need to remove the @ operator in front of the function, and then remove the error handling function handle_error() function, and then we can see the error message on the web page.

Modify the permissions of the target folder
No matter what, we still found the source of the problem, which is a very happy thing. Since the user and permissions of the folder are wrong, then we only need to modify these problems:

Modify the user who owns the folder where images are stored, and change it to the user who runs apache to execute PHP scripts.
Change the permissions of the folder to 755
So who is the user running apache? We use a PHP script to obtain:

echo exec('whoami'); //Get the username that executes the file, thereby modifying the permissions of the picture folder
In this way, I got the user who executed the script as: daemon. What you get is likely to be different from mine.
Let’s modify the user who owns the folder:

chown daemon -R ~/web/hello_php/uploads
~/web/hello_php/uploads is the target path where I store the images. -R represents recursively modifying the user of the folder in this directory.

Then modify the folder permissions

chmod 775 -R ~/web/hello_php/uploads
And we’re done,

Reference reading: http://www.manongjc.com/article/1494.html

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template