Solving the problem of PHP move_uploaded_file function failing to move pictures
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)
/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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
