Home > 类库下载 > 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

高洛峰
Release: 2016-10-09 11:25:15
Original
1559 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 confirmed previously

There is no error in 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 to 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 an error occurs during the move_uploaded_file function, it will jump to the error page. When I execute the above script, the script jumps to the error page. It is obvious that there is an error. First, I checked whether there are errors 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. 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: Use PHP with caution in the code The @ operator and the
@ operator can screen out all possible problems from invalid user input or SQL queries containing an incorrect column or even a non-standard URL. Errors can be avoided, and the code may not even be checked by the user. , by itself or with errors generated by that. 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 in this case other solutions should be used. Wrong plan.

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 was using xampp when setting up the PHP development environment. Such a development kit, so the error_log file is different from what most articles on the Internet say. Finally, I found the php_error_log file under the path
(my host is ubuntu)

/opt/lampp/logs
. Of course, there are also The error_log file of apache is stored. In the php_error_log file, I saw an error problem: insufficient permissions. I finally found the error: the destination directory where we store the images does not have permissions for the user who executes PHP scripts. The user and the user who wrote the script code and created the picture folder are not the same user

In fact, we don’t have to go to so much trouble. We just need to remove the @ operator in front of the function, and then remove the error handling function handle_error() function, and then we You 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 the pictures are permanently stored, and change it to the user who runs apache and executes PHP scripts.
Change the permissions of the folder to 755
So who is the user running apache? We use the PHP script to obtain:

echo exec('whoami'); //Get the user name to execute the file, thereby modifying the picture folder The permissions
In this way, I got the user who executed the script as: daemon. What you get is likely to be different from mine.
Now 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 means recursively to this directory. The user who owns the folder modification.

Then modify the folder permissions

chmod 775 -R ~/web/hello_php/uploads
This way we are 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