如何使用php解决iis错误500上传文件

Posted

技术标签:

【中文标题】如何使用php解决iis错误500上传文件【英文标题】:how to solve iis error 500 upload file using php 【发布时间】:2013-07-12 08:55:25 【问题描述】:

我正在尝试将文件上传到基于 IIS 的服务器。这是我的代码:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
//ini_set('error_log','/httpdocs/error_log');
echo "upload_max_filesize: " . ini_get ("upload_max_filesize"). "<br>";
echo "post_max_size: " . ini_get ("post_max_size"). "<br>";
echo "memory_limit: " . ini_get ("memory_limit"). "<br>";
echo "max_file_uploads: " . ini_get ("max_file_uploads"). "<br>";
echo "max_execution_time: " . ini_get ("max_execution_time"). "<br>";
echo "max_input_time: " . ini_get ("max_input_time"). "<br>";
echo "file_uploads: " . ini_get ("file_uploads"). "<br>";
echo "default_socket_timeout: " . ini_get ("default_socket_timeout"). "<br>";
echo "<br>";
if ($_FILES["file"]["error"] > 0)
  
  echo "Error: " . $_FILES["file"]["error"] . "<br>";
  
else
  
  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  
?>
<html>
<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

当我尝试上传大于 13MB 的文件时,我曾经收到此错误:

服务器错误

500 - 内部服务器错误。你的资源有问题 正在寻找,无法显示。

然后我添加了error_reporting,我得到了这个:

网络错误(tcp_error)

发生通信错误:“无法发送更多” Web 服务器可能 情绪低落、太忙或遇到其他问题阻止它 响应请求。您不妨稍后再试。

如需帮助,请联系您的网络支持团队。

这是设置:

upload_max_filesize: 20M

post_max_size: 128M

memory_limit:-1

max_file_uploads: 20

max_execution_time: 50000

最大输入时间:50000

file_uploads:开启

default_socket_timeout:6000

这是 web.config 文件:

<?xml version="1.0"?>
<!-- 
    Note: As an alternative to hand editing this file you can use the 
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in 
    machine.config.comments usually located in 
    \Windows\Microsoft.Net\Framework\vx.x\Config 
-->
<configuration>
  <!--
    <configSections>
      <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
          <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
            <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
            <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
            <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
          </sectionGroup>
        </sectionGroup>
      </sectionGroup>
    </configSections>  
-->
  <appSettings />
  <system.web>
    <httpRuntime 
        executionTimeout="9000" 
        maxRequestLength="65536" 
        useFullyQualifiedRedirectUrl="false" 
        minFreeThreads="8" 
        minLocalRequestFreeThreads="4" 
        appRequestQueueLimit="1000" 
        enableVersionHeader="true" 
    />
    <customErrors mode="On" />
    <!-- 
            Set compilation debug="true" to insert debugging 
            symbols into the compiled page. Because this 
            affects performance, set this value to true only 
            during development.
        -->
    <!--
            The <authentication> section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
        -->
    <authentication mode="Forms" />
    <!--
            The <customErrors> section enables configuration 
            of what to do if/when an unhandled error occurs 
            during the execution of a request. Specifically, 
            it enables developers to configure html error pages 
            to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
    <pages>
      <controls>
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </controls>
    </pages>
    <httpHandlers>
      <remove verb="*" path="*.asmx" />
      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false" />
    </httpHandlers>
    <httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </httpModules>
    <sessionState timeout="2880" />
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <providerOption name="CompilerVersion" value="v3.5" />
        <providerOption name="WarnAsError" value="false" />
      </compiler>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <providerOption name="CompilerVersion" value="v3.5" />
        <providerOption name="OptionInfer" value="true" />
        <providerOption name="WarnAsError" value="false" />
      </compiler>
    </compilers>
  </system.codedom>
  <!-- 
        The system.webServer section is required for running ASP.NET AJAX under Internet
        Information Services 7.0.  It is not necessary for previous version of IIS.
    -->
  <system.webServer>
    <security>
        <requestFiltering>
           <requestLimits maxAllowedContentLength="2147483648" />
        </requestFiltering>
    </security>
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
      <remove name="ScriptModule" />
      <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </modules>
    <handlers>
      <remove name="WebServiceHandlerFactory-Integrated" />
      <remove name="ScriptHandlerFactory" />
      <remove name="ScriptHandlerFactoryAppServices" />
      <remove name="ScriptResource" />
      <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

我远程访问了服务器并对其进行了测试,响应是:

PHP 警告:9202605 字节的 POST 内容长度超出限制 第 0 行未知中的 8388608 字节

我该如何解决这个问题?

【问题讨论】:

只是一个想法,如果您在代理服务器后面运行它(通常在办公室局域网中),它可能会阻止文件上传超过一定大小。 不,我从外部网络尝试过 您应该将帖子大小限制为 8 m,这是 ini 的默认限制或增加 post_max_size 为:128M 【参考方案1】:

据我所知,这不是 php 的直接问题,而是 IIS 的问题。 您可以尝试将其添加到 web.config 文件中,并将其添加到您的 php 项目 Web 服务器根目录中。

<system.webServer>
    <security>
        <requestFiltering>
           <requestLimits maxAllowedContentLength="2147483648" />
        </requestFiltering>
    </security>
</system.webServer>

【讨论】:

您能否确认文件上传错误与文件大小有关。您应该转储 $_FILE 并根据此处列出的代码检查错误代码,link 您还可以确认上述 sn-p 对于您的 IIS 版本是可以的【参考方案2】:

虽然我从 cPanel 更改了 PHP 设置,但每当我使用 ini_get() 时,它给了我在 cPanel 中的设置,但它不起作用。

我解决了这个问题(如果您可以访问php.ini 文件),方法是从 php.ini 文件本身而不是 cPanel 更改设置。

【讨论】:

【参考方案3】:

问题: 在将 Windows Server 2008 和 IIS 作为 Web 服务器运行时,某些 php 文件会导致“HTTP 500 - 内部服务器错误”。

修复: 我们发现使用 FastCGI 协议将简单的 php 通知(即未定义的变量)发送到 stderr 并导致 IIS FastCGI 模块请求失败并显示 HTTP 500 错误。禁用 FastCGI 日志记录将阻止 PHP 通过 stderr 发送错误信息,并为客户端生成 500 个响应代码。

要解决此问题,请确保在您的 php.ini 文件中将以下设置设置为 0:

fastcgi.logging=0

更多关于 IIS 上的 FastCGI 和 PHP 的信息可以在这里找到:

http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/

来源:http://www.redcart.com/forums/showthread.php?t=377

【讨论】:

以上是关于如何使用php解决iis错误500上传文件的主要内容,如果未能解决你的问题,请参考以下文章

IIS+php服务器无法上传图片(500 错误)

iis 打开ASP文件报错500

php上传文件出现500错误

PHP 大文件上传解决方案(500M以上)

php上传文件错误怎么解决

thinkcmf 上传服务器报500错误 提示 css样式和图片无法正常获取显示?