php – 如何在不使用太多内存的情况下强制下载大文件?

前端之家收集整理的这篇文章主要介绍了php – 如何在不使用太多内存的情况下强制下载大文件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试向用户提供大型zip文件.当有2个并发连接时,服务器内存不足(RAM).我将内存量从300MB增加到4GB(Dreamhost VPS)然后它运行正常.

我需要允许超过2个并发连接.实际的4GB将允许20个并发连接(太糟糕).

好吧,我正在使用的当前代码需要内存的两倍,然后是实际的文件大小.这太糟糕了.我希望将文件“流式传输”给用户.因此,我将分配的不仅仅是为用户提供的块.

以下代码是我在CodeIgniter(PHP框架)中使用的代码

  1. ini_set('memory_limit','300M'); // it was the maximum amount of memory from my server
  2. set_time_limit(0); // to avoid the connection being terminated by the server when serving bad connection downloads
  3. force_download("download.zip",file_get_contents("../downloads/big_file_80M.zip"));exit;

force_download函数如下(CodeIgniter默认帮助函数):

  1. function force_download($filename = '',$data = '')
  2. {
  3. if ($filename == '' OR $data == '')
  4. {
  5. return FALSE;
  6. }
  7.  
  8. // Try to determine if the filename includes a file extension.
  9. // We need it in order to set the MIME type
  10. if (FALSE === strpos($filename,'.'))
  11. {
  12. return FALSE;
  13. }
  14.  
  15. // Grab the file extension
  16. $x = explode('.',$filename);
  17. $extension = end($x);
  18.  
  19. // Load the mime types
  20. @include(APPPATH.'config/mimes'.EXT);
  21.  
  22. // Set a default mime if we can't find it
  23. if ( ! isset($mimes[$extension]))
  24. {
  25. $mime = 'application/octet-stream';
  26. }
  27. else
  28. {
  29. $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
  30. }
  31.  
  32. // Generate the server headers
  33. if (strpos($_SERVER['HTTP_USER_AGENT'],"MSIE") !== FALSE)
  34. {
  35. header('Content-Type: "'.$mime.'"');
  36. header('Content-Disposition: attachment; filename="'.$filename.'"');
  37. header('Expires: 0');
  38. header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
  39. header("Content-Transfer-Encoding: binary");
  40. header('Pragma: public');
  41. header("Content-Length: ".strlen($data));
  42. }
  43. else
  44. {
  45. header('Content-Type: "'.$mime.'"');
  46. header('Content-Disposition: attachment; filename="'.$filename.'"');
  47. header("Content-Transfer-Encoding: binary");
  48. header('Expires: 0');
  49. header('Pragma: no-cache');
  50. header("Content-Length: ".strlen($data));
  51. }
  52.  
  53. exit($data);
  54. }

我尝试了一些我在Google中找到的基于块的代码,但文件总是被破坏了.可能是因为代码不好.

谁能帮助我?

this thread中有一些想法.我不知道readfile()方法是否会节省内存,但听起来很有希望.

猜你在找的PHP相关文章