The IIS + php server cannot upload images. Solution

Source: Internet
Author: User
Tags file upload iis ini php server php tutorial

IIS + php Tutorial server cannot upload images solution

On the server, Apache2 + PHP runs normally and is replaced with IIS + PHP. the ini environment variables cannot be read, and the verification code in php cannot be displayed. Some people are now unable to upload images.

The process of replacing IIS with Apache2 is only to enable IIS and disable Apache2. There are so many differences, but there are still many changes in IIS support for PHP.

Analysis:

According to the above description, I suspect that the problem lies in the IIS permission configuration. The IUSR_MACHINE account has no write permission on upload, so the permission is modified, all permissions under NTFS are modified, but they are useless after all, and there is no corresponding information on the network. The upload page is tested in the following process:

The swf file calls save. php to upload the file ----> the swf file renames the uploaded file ---> The name is returned to save. php ---> and the last name is displayed.

The problem persists until swf renames the file. The final name is not displayed, and the swf file is not involved in the upload process. the PHP file finds the problem and tests it in the file. The variable used for the last name displayed is fileName. Then, insert the following statement for testing:

Echo "fileName = 2008 *****. gif ";

The function of this sentence is to make fileName have a value. save. php can be displayed normally. First, the original statement should be used for blocking testing, and the results will be returned normally. However, when the Test reaches:
If (! @ Move_uploaded_file ($ f ["tmp_name"], $ dest_dir. '/'. $ fileName) header ("HTTP/1.0 404 Not Found ");
In this case, the problem occurs. You cannot upload the file, search for the context, and never find the tmp_name variable. However, it means to upload the file to a temporary file first, move it to the target location. Where is the tmp location? Is it because this location cannot be written that the file cannot be uploaded?

Search for online materials and find that php. ini contains two parts about the upload configuration:
File_uploads = On: Specifies whether HTTP upload is allowed. The default value is ON.
; Upload_tmp_dir = set the temporary location for storing uploaded files

Relevant information about the two locations on the Internet is:
I try to set up file uploading under IIS 7 and PHP 5.

First problem was to set 2 variables in php. ini

File_uploads = On // Here, the php. Ini file is set to On

Upload_tmp_dir = "C: Inetpubwwwrootuploads" // This path is the temporary storage path for uploaded files.

For some reasons such directory name works,
But "upload_tmp" won't work.

The second problem was to set correct user rigths for upload folders where you try to save your file. I set my upload folder rights for the "WORKGROUP/users" for the full access. you may experiment by yourselves if you not need execute access, for example.

In my php. ini, upload_tmp_dir is commented out, not enabled, and not set. Why does Apache2 Upload normally? Is the problem true?

Solution:

Create a folder for temporary Upload directory, and modify php according to the instructions above. in ini, set the temporary Upload directory upload_tmp_dir to the folder you just created, grant the folder permission to the "IUSR _ computer name" user writable, and restart IIS, try uploading. The problem is solved in this way.


The final analysis answer:

The above content was written in, but now I added a new server in, and this problem occurs again. At the same time, I implemented it again according to the above solution, in the course of the operation, it was probably because of an error that failed, and I had to take some time to study the specific cause. I found the cause of the problem as follows.
Files cannot be uploaded. It does not mean that all files cannot be uploaded because flash calls fwrite () to upload portraits on one of my websites, however, call @ move_uploaded_file ($ f ["tmp_name"], $ dest_dir. '/'. $ fileName.
After my analysis, the reason is that fwrite () is a binary file, while move_uploaded_file () is a text file, the windows operating system distinguishes these two types of files [refer to the description of the fwrite () function in the php Manual], that is to say, the temporary Upload directories stored for these two different files are different in the php environment, because when configuring PHP in the IIS environment, the temporary directory is set to E: tmp, and the iusr user can write the directory. The binary file can be uploaded. Therefore, I suspect that this directory is the storage location of the temporary files uploaded to the binary file, so where is the temporary storage location of text files transmitted by move_uploaded_file? Actually, in the above English section, the path set by upload_tmp_dir is the same, but in my servers, the value of this setting for each server is commented out as "no value ", why can't some servers Upload files? This is back to my previous question. Why can Apache2 be uploaded but not iis?
This time I analyze the upload. Php file again and analyze the code that causes the fault as follows:

// Check whether a file is uploaded
If (! $ _ FILES ['upload'. $ num] ['name'] = ""){
If ($ _ FILES ['upload'. $ num] ['size'] <$ max_size ){
1. echo "file upload path:". $ location. $ _ FILES ['upload'. $ num] ['name'];
2. echo "temporary File name:". $ _ FILES ['upload'. $ num] ['tmp _ name'];
3. move_uploaded_file ($ _ FILES ['upload '. $ num] ['tmp _ name'], $ location. $ _ FILES ['upload '. $ num] ['name']) or $ event = "Failure ";
} Else {
$ Event = "File too large! ";
    }

The first sentence in the normal code does not exist. For the convenience of testing, I added it to test my php. when ini does not set the value of upload_dir_tmp, the uploaded file is temporarily stored. After this test, it is found that php is not configured. the default storage location for ini's upload_dir_tmp value is in the C: windowstemp directory, and the temporary file is. tmp is stored with the suffix and the file will be deleted immediately. Therefore, you cannot find the default path of upload_dir_tmp if you want to modify the search function of the file in the operating system.

Now that the default path of upload_dir_tmp is found, modify the access permission of c: windowstemp, Grant IUSR _ users writable, restart the IIS Admin service, and upload files. This is why when the values of upload_dir_tmp on multiple servers are empty, some can be uploaded or some cannot be uploaded.

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.