Home  >  Article  >  Operation and Maintenance  >  Solution to the problem that PHP configured with IIS can be used under Windows but cannot upload files

Solution to the problem that PHP configured with IIS can be used under Windows but cannot upload files

黄舟
黄舟Original
2017-05-27 09:48:062084browse

PHP configured using IIS under cannot upload files, which has always been a confusing point for many netizens. I have collected and sorted it out, hoping it can help you

Continuation of the article "Configuring PHP with iis in Windows Server 2003"
The server runs normally using Apache2+PHP, and when it is replaced with IIS+PHP, the environment of php.ini appears successively Variables cannot be read, in php Verification code cannot be displayed. Now some people have reported the problem of being unable to upload pictures.

The process of replacing Apache2 from IIS is just to turn on IIS and turn off Apache2. No other changes, but there are so many differences. It seems that IIS still has a lot to do to support PHP. Modified.
Analysis:
Based on the above description, I suspect that the problem lies in the permission configuration of IIS. The IUSR_MACHINE account does not have write permissions for upload, so I modified the permissions and set the permissions under IIS. , modified the permissions under NTFS, but in the end it was useless. There was no corresponding information when searching for information on the network. Test the upload page. The process is:
swf file calls save.php to upload the file----> ;swf file renames the uploaded file--->The name is returned to save.php--->The last name is displayed.

The current problem has been stuck in the renaming of the file by swf. The last name has not been displayed, and the swf file does not participate in the upload process. Then the problem can only be found in the save.php file. Now, test in this file. The variable used for the last displayed name is fileName, so insert the following statement for testing:
echo "fileName=2008* ****.gif";
The function of this sentence is to make fileName have a value and save.php can be displayed normally. First, shield the original statement one by one and test it. Everything is normal. It returned, but when the test reached:

if (mailto:!@move_uploaded_file($f[%22tmp_name"], $dest_
dir
.'/'.$fileName)) 
head
er("HTTP/1.0 404 Not Found");

File upload to a temporary file, and then moved to the destination location, where is the tmp location? Is this location unwritable and
causing the file to be unable to be uploaded?
Searching for online information, I found that there are 2 places under php.ini for upload configuration:
file_uploads = On Here, set whether to allow HTTP upload, the default should be ON
;upload_tmp_dir= Here, set the upload file storage The temporary location
The relevant information on the Internet for these two places 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 //这里是说php.ini文件这个地方设置成On 
upload_tmp_dir = "C:\Inetpub\wwwroot\uploads" //这个路径就是自己设置的上传文件临时存储路径 
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 
right
s for the "WORKGROUP/users" for the full access. You may experiment by yourselves if you not need execute access, for example.

我的php.ini中upload_tmp_dir是被注释的,没有启用,更没有设置,可是为什么Apache2却可以正常上传呢?难道问题真的出在这里?

解决
新建一个文件夹做临时上传目录,按照上面的英文说明修改php.ini中相应的那2项,把临时上传目录upload_tmp_dir设置成刚才建立的文件夹,把该文件夹的权限赋予“IUSR_计算机名”用户可写,重新启动IIS,上传试试,问题真的就这样解决了。

最终的分析答案
上面的内容写于09年,但是现在2010年7月我新增一台服务器,又出现了这个问题,同时再次按照上面的解决方法实施,在操作的过程中大概是由于哪里出了错,竟然没有成功,不得不抽出点时间来研究具体原因,找到了最终产生这个问题的原因如下。
无法上传文件,不代表所有文件都无法上传,因为我的一个网站,flash调用fwrite()传头像之类的成功了,但是调用@move_uploaded_file($f["tmp_name"], $dest_dir.'/'.$fileName)这样的函数传照片的时候仍旧无法上传。

经过我的分析,原因是由于fwrite()是传的二进制文件,而move_uploaded_file()传的是文本文件,而windows操作系统是区分这2种文件的 [参考php手册fwrite()函数的说明],这也就是说这2种不同的文件在php环境下上传时所存储的临时上传目录是不同的,由于在配置IIS环境下的PHP的时候,设置的临时目录为E:\tmp,设置该目录的iusr用户可写,二进制文件即可上传,所以我怀疑该目录就是二进制文件上传临时文件的存储位置,那么move_uploaded_file()传的文本文件的临时文件存储位置在哪里呢?其实就是在上面的那段英文里面,upload_tmp_dir设置的路径就是了,但是我的几台服务器中,每台服务器的这个设置的值都是被注释掉的“no value”,为什么有的服务器可以上传,而有的服务器不可以上传呢?这也就回到了以前我提出的问题,为什么Apache2可以上传而iis不可以上传呢?
这次我再次分析upload.php文件,分析其中造成该故障的代码具体内容如下: 

代码如下:

// 检查是否有文件上传 
if (! $_FILES['upload'.$num]['name'] == ""){ 
if ($_FILES['upload'.$num]['size'] < $max_size) { 
1、 echo "文件上传路径:".$location.$_FILES['upload'.$num]['name']; 
2、echo "文件临时文件名:".$_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!"; 
}


其中正常代码中第2句是不存在的,为了测试方便我加上来的,它的主要目的就是测试我的php.ini没有设置upload_dir_tmp的值的时候,上传的文件临时保存在哪里的,经过这个测试发现原来在不配置php.ini的upload_dir_tmp的值的时候,默认的存储位置是在C:\windows\temp目录,并且临时文件是以.tmp为后缀存储的,该文件马上就会被删除,所以你想通过操作系统的文件修改搜索功能是无法找到的,也就无法找到upload_dir_tmp的默认路径是哪里。

既然找到了upload_dir_tmp的默认路径了,那么修改c:\windows\temp的访问权限,赋予IUSR_用户可写,重启动IIS Admin服务,上传文件,终于成功了。这就是为什么我的多台服务器upload_dir_tmp的值都为空的时候有的可传,有的不可传的原因。

现在已经修改了IIS使用环境变量方式精简配置php的那篇文章了,因为当时没有注意这个位置的权限设置,造成了如今的问题,不过最终解决也是好的。

The above is the detailed content of Solution to the problem that PHP configured with IIS can be used under Windows but cannot upload files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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