Home Operation and Maintenance Windows 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

May 27, 2017 am 09:48 AM

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");
Copy after login

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.
Copy after login

我的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[&#39;upload&#39;.$num][&#39;size&#39;] < $max_size) { 
1、 echo "文件上传路径:".$location.$_FILES[&#39;upload&#39;.$num][&#39;name&#39;]; 
2、echo "文件临时文件名:".$_FILES[&#39;upload&#39;.$num][&#39;tmp_name&#39;]; 
3、 move_uploaded_file($_FILES[&#39;upload&#39;.$num][&#39;tmp_name&#39;],$location.$_FILES[&#39;upload&#39;.$num][&#39;name&#39;]) or $event = "Failure"; 
} 
else
 { 
$event = "File too large!"; 
}
Copy after login


其中正常代码中第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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP: Is It Dying or Simply Adapting? PHP: Is It Dying or Simply Adapting? Apr 11, 2025 am 12:13 AM

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

See all articles