【问题标题】:Upload large files and Time out php uploads上传大文件和超时 php 上传
【发布时间】:2018-08-29 04:22:30
【问题描述】:

客户的站点上传文件,但如果这些文件很大,则会返回超时错误503 Service Unavailable

hopedagem 将超时时间限制在300秒,请问有没有什么方法可以通过js或者相关的方式上传不超时? 上传的是视频。

The hosting server does not allow time out editing.

【问题讨论】:

    标签: php


    【解决方案1】:

    转到php.ini 文件并根据您的要求更改值。

    upload_max_filesize

    默认情况下,此值为 2M。我们需要将其增加到我们要上传的单个文件的最大大小。

    最大输入时间

    这将设置允许脚本解析输入数据的最长时间(以秒为单位),例如 POSTGET。计时从服务器调用 PHP 的那一刻开始,到执行开始时结束。这将包括填充$_FILES superglobal

    memory_limit

    这设置了 PHP 脚本在执行期间允许使用的内存量。将此设置为大于“post_max_size”的值,以便 PHP 脚本可以加载和处理上传的文件。

    post_max_size

    它定义了 PHP 将接受的 POST 数据的最大大小。此值应大于“upload_max_filesize”。

    ma​​x_execution_time

    脚本在其输入被解析后允许运行的时间。这将包括对文件本身的任何处理。

    如果您遇到与内存相关的错误,请关闭输出缓冲,要考虑的 PHP 配置指令是“output_buffering

    output_buffering = Off
    

    我还添加了上述配置的参考,您可以找到更多详细信息here

    【讨论】:

    • 嗨,谢谢我的朋友。服务器不允许编辑max_execution_time 限制为300秒,我已经编辑了其他项但是没有成功
    • 如果您无法在服务器端进行更改,那么您可以将此行添加到 php 脚本或文件 ini_set('max_execution_time', 300); //300 seconds = 5 minutes
    【解决方案2】:

    您需要更改 php.ini 中的一些设置:

    upload_max_filesize = 500M 
    ;or whatever size you want
    
    max_execution_time = 1000
    ; also, higher if you must - sets the maximum time in seconds
    

    【讨论】:

      【解决方案3】:

      已解决:https://www.plupload.com/

          <div id="uploader">
              <p>Your browser doesnt have Flash, Silverlight or HTML5 support.</p>
          </div>
      
      
          <link href="https://rawgithub.com/moxiecode/plupload/master/js/jquery.plupload.queue/css/jquery.plupload.queue.css" type="text/css" rel="stylesheet" media="screen">
          <script type="text/javascript" src="assets/uploads/jquery-ui.js"></script>
          <script type="text/javascript" src="assets/uploads/plupload.full.min.js"></script>
          <script type="text/javascript" src="assets/uploads/jquery.plupload.queue/jquery.plupload.queue.min.js"></script>
          <script type="text/javascript" src="assets/uploads/jquery.ui.plupload/jquery.ui.plupload.min.js"></script>
          <link type="text/css" rel="stylesheet" href="assets/uploads/jquery.ui.plupload/css/jquery.ui.plupload.css" media="screen" />
      
          <script type="text/javascript" src="assets/uploads/i18n/pt_BR.js"></script>
      
      
          <script type="text/javascript">
          $(function() {
              $("#uploader").plupload({
                  // General settings
                  runtimes : 'html5,flash,silverlight,html4',
                  url : "controller/xml_upload.php",
      
                  // Maximum file size
                  max_file_size : '2048mb',
      
                  chunk_size: '1mb',
      
                  // Specify what files to browse for
                  filters : [
                      {title : "XML", extensions : "xml"}
                  ],
      
                  // Rename files by clicking on their titles
                  rename: true,
      
                  // Sort files
                  sortable: true,
      
                  // Enable ability to drag'n'drop files onto the widget (currently only HTML5 supports that)
                  dragdrop: false,
      
                  // Views to activate
                  views: {
                      list: false,
                      thumbs: false, // Show thumbs
                      active: 'thumbs'
                  },
      
                  // Flash settings
                  flash_swf_url : '/videos_add1_up/Moxie.swf',
      
                  // Silverlight settings
                  silverlight_xap_url : '/videos_add1_up/Moxie.xap',
      
                  multipart_params : {
                      "xml_usuario" : "xml_user"
                  }
              });
          });
          </script>
      

      PHP

          header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
          header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
          header("Cache-Control: no-store, no-cache, must-revalidate");
          header("Cache-Control: post-check=0, pre-check=0", false);
          header("Pragma: no-cache");
          @set_time_limit(5 * 60);
          $targetDir = '../../assets/arquivos/xml/';
          $cleanupTargetDir = true; 
          $maxFileAge = 5 * 3600; 
      
          if (!file_exists($targetDir)) {
              @mkdir($targetDir);
          }
      
          if (isset($_REQUEST["name"])) {
              $fileName = $_REQUEST["name"];
          } elseif (!empty($_FILES)) {
              $fileName = $_FILES["file"]["name"];
          } else {
              $fileName = uniqid("file_");
          }
      
      
      
      
          include '../../_config.php';
      
          $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
      
          // Chunking might be enabled
          $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
          $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
      
      
          if ($cleanupTargetDir) {
              if (!is_dir($targetDir) || !$dir = opendir($targetDir)) {
                  die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
              }
      
              while (($file = readdir($dir)) !== false) {
                  $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
      
                  // If temp file is current file proceed to the next
                  if ($tmpfilePath == "{$filePath}.part") {
                      continue;
                  }
      
                  // Remove temp file if it is older than the max age and is not the current file
                  if (preg_match('/\.part$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge)) {
                      @unlink($tmpfilePath);
                  }
              }
              closedir($dir);
          }   
      
      
          if (!$out = @fopen("{$filePath}.part", $chunks ? "ab" : "wb")) {
              die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
          }
      
          if (!empty($_FILES)) {
              if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {
                  die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
              }
      
              // Read binary input stream and append it to temp file
              if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) {
                  die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
              }
          } else {    
              if (!$in = @fopen("php://input", "rb")) {
                  die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
              }
          }
      
          while ($buff = fread($in, 4096)) {
              fwrite($out, $buff);
          }
      
          @fclose($out);
          @fclose($in);
      
          // Check if file has been uploaded
          if (!$chunks || $chunk == $chunks - 1) {
              // Strip the temp .part suffix off 
              rename("{$filePath}.part", $filePath);
          }
      

      【讨论】:

        猜你喜欢
        • 2012-02-02
        • 2011-11-13
        • 1970-01-01
        • 2012-08-07
        • 2014-08-20
        • 2023-03-06
        • 1970-01-01
        • 2012-07-20
        • 1970-01-01
        相关资源
        最近更新 更多