An error occurred while moving the image through the PHP move_uploaded_file function,

Source: Internet
Author: User
Tags php development environment

An error occurred while moving the image through the PHP move_uploaded_file function,

Problem description:
Today, when implementing a PHP script for uploading profile picture files during user registration, a problem occurs: the PHP script has been confirmed before

There is no error in uploading files on the browser side.
The uploaded file is valid.
The uploaded file is an image file.
A unique file name has been generated on the server.
Code
Next, we should move the file from the temporary location to a fixed location, so I wrote the following script:

// Move the file from the temporary location to a fixed location @ move_uploaded_file ($ _ FILES [$ image_fieldname] ['tmp _ name'], $ upload_filename) or handle_error ("error in storing image files", "error in Moving Files ". "{$ upload_filename }");
The Code handle_error () function is an error handling function defined by myself. When the move_uploaded_file function fails to be executed, the error page is displayed. When I execute the above script, the script jumps to the error page, which is obviously an error. First, I checked whether my function parameters were incorrect:

$ _ FILES [$ image_fieldname] ['tmp _ name'] $ upload_filename // It is the file path of my own combination. Make sure it is correct.
According to the PHP manual, the above two parameters of the input function can ensure that there is no problem. What is the problem, no error is reported on the page (I used the "@" operator of PHP before the function, so the page will not report an error)

@ Operator

Note: Use the @ operator of PHP with caution in the code,
@ Operator can avoid all problems that may result from invalid user input, SQL queries containing an incorrect column, or even an invalid URL error, the code can even not check errors generated by the user, yourself, or the band. In short, the @ operator can block error messages of the Code, which is often used by a popular website @, because they cannot crash or stop at all, but in this case, you need to use other solutions to solve the error.

Search for the error log File
At that time, I was not aware of the @ operator blocking error information. I just wanted to look for the error log file of apache, because I used Development Kits like xampp when setting up a PHP development environment, the error_log file is different from most articles on the Internet.
(My host is ubuntu)

/Opt/lampp/logs
The php_error_log file is found in the path. Of course, the apache error_log file is also stored in this path. In the php_error_log file, I see an error: the permission is insufficient. I finally found the error: the target directory for storing images does not have permissions for PHP users. Users who execute PHP scripts are not the same as those who write script code and Create Image folders.

In fact, we don't need to be so troublesome. We just need to remove the @ operator before the function, and then remove the handle_error () function of the error processing function. Then we can see the error information on the web page.

Modify target folder Permissions
In any case, we still find the root cause of the problem. This is a great pleasure. Since the user and permission of the folder are incorrect, we only need to modify these questions:

Modify the user of the fixed storage folder of the image to run apache to execute the PHP script.
Change the folder permission to 755
Who is the user who runs apache? We can use the PHP script to get it:

Echo exec ('whoamam'); // obtain the user name for executing the file to modify the permissions of the image folder.
In this way, the user who executes the script is daemon. You may get something different from mine.
Next we will modify the user to which the folder belongs:

Chown daemon-R ~ /Web/hello_php/uploads
~ /Web/hello_php/uploads is the target path for storing images.-R indicates recursively modifying the user of the folder in this directory.

Next, modify the folder permission.

Chmod 775-R ~ /Web/hello_php/uploads
In this way, we are done,

 

Read: http://www.manongjc.com/article/1494.html

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.