PHP文件下载问题 [英] PHP File Downloading Questions

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

问题描述

我目前遇到用户下载存储在我的服务器上的文件的一些问题。我的代码设置为自动下载文件一旦用户点击下载按钮。它适用于所有文件,但是当大小超过30 MB时,它有问题。用户下载有限吗?另外,我提供了我的示例代码,我想知道是否有比使用PHP函数file_get_contents更好的做法。

I am currently running into some problems with user's downloading a file stored on my server. I have code set up to auto download a file once the user hits the download button. It is working for all files, but when the size get's larger than 30 MB it is having issues. Is there a limit on user download? Also, I have supplied my example code and am wondering if there is a better practice than using the PHP function 'file_get_contents'.

感谢大家的帮助! p>

Thank You all for the help!

$path = $_SERVER['DOCUMENT_ROOT'] . '../path/to/file/';
$filename = 'filename.zip';
$filesize = filesize($path . $filename);
@header("Content-type: application/zip");
@header("Content-Disposition: attachment; filename=$filename");
@header("Content-Length: $filesize")
echo file_get_contents($path . $filename);


推荐答案

file_get_contents() 将整个文件加载到内存中 - 使用一个日志。

file_get_contents() will load the whole file into memory -- using a log of it.

而且,在PHP中,脚本可以使用的内存量是有限的(请参阅 memory_limit / a>) - 这可能解释了您的下载脚本不适用于大文件。

And, in PHP, the amount of memory a script can used is limited (see memory_limit) -- which might explain that your download script doesn't work for big files.



使用
readfile() ,相反,可能是一个更好的选择:它会读取文件,并直接将其内容发送到输出缓冲区。


Using readfile(), instead, might be a better choice : it will read the file, and directly send its content to the output buffer.

这意味着:


  • 不将整个文件加载到内存中

  • 不必回显您在内存中加载的内容。

只需使用这样的东西就可以了:

Just using something like this should be OK :

$path = $_SERVER['DOCUMENT_ROOT'] . '../path/to/file/';
$filename = 'filename.zip';
$filesize = filesize($path . $filename);
@header("Content-type: application/zip");
@header("Content-Disposition: attachment; filename=$filename");
@header("Content-Length: $filesize")
readfile($path . $filename);



(BTW:你真的想沉默错误吗方式,使用 @ 运算符?另一个解决方案可能是不显示它们,但将其记录到文件中 - 请参阅 display_errors log_errors error_log


(BTW : do you really want to silence errors this way, with the @ operator ? Another solution could be to not display them, but log them to a file -- see display_errors, log_errors, and error_log)

这篇关于PHP文件下载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆