• <i id='3uCBz'><tr id='3uCBz'><dt id='3uCBz'><q id='3uCBz'><span id='3uCBz'><b id='3uCBz'><form id='3uCBz'><ins id='3uCBz'></ins><ul id='3uCBz'></ul><sub id='3uCBz'></sub></form><legend id='3uCBz'></legend><bdo id='3uCBz'><pre id='3uCBz'><center id='3uCBz'></center></pre></bdo></b><th id='3uCBz'></th></span></q></dt></tr></i><div id='3uCBz'><tfoot id='3uCBz'></tfoot><dl id='3uCBz'><fieldset id='3uCBz'></fieldset></dl></div>
    <legend id='3uCBz'><style id='3uCBz'><dir id='3uCBz'><q id='3uCBz'></q></dir></style></legend>

    1. <small id='3uCBz'></small><noframes id='3uCBz'>

        • <bdo id='3uCBz'></bdo><ul id='3uCBz'></ul>
      1. <tfoot id='3uCBz'></tfoot>
      2. 用 PHP 可靠地下载大文件

        Downloading large files reliably in PHP(用 PHP 可靠地下载大文件)

        <small id='bn8G4'></small><noframes id='bn8G4'>

          <bdo id='bn8G4'></bdo><ul id='bn8G4'></ul>
        • <tfoot id='bn8G4'></tfoot>
                    <tbody id='bn8G4'></tbody>
                  <i id='bn8G4'><tr id='bn8G4'><dt id='bn8G4'><q id='bn8G4'><span id='bn8G4'><b id='bn8G4'><form id='bn8G4'><ins id='bn8G4'></ins><ul id='bn8G4'></ul><sub id='bn8G4'></sub></form><legend id='bn8G4'></legend><bdo id='bn8G4'><pre id='bn8G4'><center id='bn8G4'></center></pre></bdo></b><th id='bn8G4'></th></span></q></dt></tr></i><div id='bn8G4'><tfoot id='bn8G4'></tfoot><dl id='bn8G4'><fieldset id='bn8G4'></fieldset></dl></div>

                  <legend id='bn8G4'><style id='bn8G4'><dir id='bn8G4'><q id='bn8G4'></q></dir></style></legend>

                  本文介绍了用 PHP 可靠地下载大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在服务器上有一个 php 脚本可以将文件发送给配方:他们获得一个唯一的链接,然后他们可以下载大文件.有时传输会出现问题,文件已损坏或无法完成.我想知道是否有更好的方法来发送大文件

                  I have a php script on a server to send files to recipents: they get a unique link and then they can download large files. Sometimes there is a problem with the transfer and the file is corrupted or never finishes. I am wondering if there is a better way to send large files

                  代码:

                  $f = fopen(DOWNLOAD_DIR.$database[$_REQUEST['fid']]['filePath'], 'r');
                  while(!feof($f)){
                      print fgets($f, 1024);
                  }
                  fclose($f);
                  

                  我见过诸如

                  http_send_file
                  http_send_data
                  

                  但我不确定它们是否会起作用.

                  But I am not sure if they will work.

                  解决此问题的最佳方法是什么?

                  What is the best way to solve this problem?

                  问候
                  埃尔温

                  推荐答案

                  分块文件是 PHP 中最快/最简单的方法,如果你不能或不想使用像 cURL, mod-xsendfile 在 Apache 或一些 专用脚本.

                  Chunking files is the fastest / simplest method in PHP, if you can't or don't want to make use of something a bit more professional like cURL, mod-xsendfile on Apache or some dedicated script.

                  $filename = $filePath.$filename;
                  
                  $chunksize = 5 * (1024 * 1024); //5 MB (= 5 242 880 bytes) per one chunk of file.
                  
                  if(file_exists($filename))
                  {
                      set_time_limit(300);
                  
                      $size = intval(sprintf("%u", filesize($filename)));
                  
                      header('Content-Type: application/octet-stream');
                      header('Content-Transfer-Encoding: binary');
                      header('Content-Length: '.$size);
                      header('Content-Disposition: attachment;filename="'.basename($filename).'"');
                  
                      if($size > $chunksize)
                      { 
                          $handle = fopen($filename, 'rb'); 
                  
                          while (!feof($handle))
                          { 
                            print(@fread($handle, $chunksize));
                  
                            ob_flush();
                            flush();
                          } 
                  
                          fclose($handle); 
                      }
                      else readfile($path);
                  
                      exit;
                  }
                  else echo 'File "'.$filename.'" does not exist!';
                  

                  移植自 richnetapps.com/NeedBee.对 200 MB 文件进行了测试,其中 readfile() 死亡,即使最大允许内存限制设置为 1G,这是下载文件大小的五倍.

                  Ported from richnetapps.com / NeedBee. Tested on 200 MB files, on which readfile() died, even with maximum allowed memory limit set to 1G, that is five times more than downloaded file size.

                  顺便说一句:我也在文件 >2GB 上测试了这个,但 PHP 只设法写入了第一个 2GB 文件,然后断开了连接.与文件相关的函数(fopen、fread、fseek)使用 INT,因此您最终会达到 2GB 的限制.在这种情况下,上述解决方案(即 mod-xsendfile)似乎是唯一的选择.

                  BTW: I tested this also on files >2GB, but PHP only managed to write first 2GB of file and then broke the connection. File-related functions (fopen, fread, fseek) uses INT, so you ultimately hit the limit of 2GB. Above mentioned solutions (i.e. mod-xsendfile) seems to be the only option in this case.

                  编辑:让自己 100% 将文件保存在 utf-8 中.如果省略,下载的文件将被损坏.这是因为该解决方案使用 print 将文件块推送到浏览器.

                  EDIT: Make yourself 100% that your file is saved in utf-8. If you omit that, downloaded files will be corrupted. This is, because this solutions uses print to push chunk of a file to a browser.

                  这篇关于用 PHP 可靠地下载大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is PHP or PHP based web framework stateful or stateless?(PHP 或基于 PHP 的 Web 框架是有状态的还是无状态的?)
                  How to parse django style template tags(如何解析 django 样式模板标签)
                  What is a good setup for editing PHP in Emacs?(在 Emacs 中编辑 PHP 的好设置是什么?)
                  How to check whether specified PID is currently running without invoking ps from PHP?(如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?)
                  What#39;s the difference between escapeshellarg and escapeshellcmd?(escapeshellarg 和escapeshellcmd 有什么区别?)
                  php in background exec() function(php 后台 exec() 函数)
                • <small id='few7r'></small><noframes id='few7r'>

                  <i id='few7r'><tr id='few7r'><dt id='few7r'><q id='few7r'><span id='few7r'><b id='few7r'><form id='few7r'><ins id='few7r'></ins><ul id='few7r'></ul><sub id='few7r'></sub></form><legend id='few7r'></legend><bdo id='few7r'><pre id='few7r'><center id='few7r'></center></pre></bdo></b><th id='few7r'></th></span></q></dt></tr></i><div id='few7r'><tfoot id='few7r'></tfoot><dl id='few7r'><fieldset id='few7r'></fieldset></dl></div>
                        <legend id='few7r'><style id='few7r'><dir id='few7r'><q id='few7r'></q></dir></style></legend>

                            <tbody id='few7r'></tbody>
                          <tfoot id='few7r'></tfoot>

                            <bdo id='few7r'></bdo><ul id='few7r'></ul>