使用 php 提供可下载文件
PHP 服务器位于公共文件夹之外的可下载文件很常见:
$fp = fopen($file,"r") ;
header("Content-Type: application/msword");
header('Content-Disposition: attachment; filename="'.$filename.'"');
while (! feof($fp)) {
$buff = fread($fp,4096);
echo $buff;
}
我有两个问题:
与 Web 服务器提供的静态文件(直接文件 URL)进行比较;这种方法是否需要更多资源(内存和CPU),因为PHP需要读取文件并传递?
如何在页面上显示一些文本?正如我们定义的
header
一样,我们没有 php 脚本的 html 输出。
It is common to server downloadable files which are located outside the public folder by PHP:
$fp = fopen($file,"r") ;
header("Content-Type: application/msword");
header('Content-Disposition: attachment; filename="'.$filename.'"');
while (! feof($fp)) {
$buff = fread($fp,4096);
echo $buff;
}
I have two questions:
Comparing with static files served by web server (direct file URL); does this method needs more resources (memory and cpu), as PHP needs to read the file and deliver?
How we can display some text on the page? As we defined
header
, we do not have html output of the php script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另外我建议也使用内容长度标头。
Also I'd suggest using the Content Length Header as well.
1 由于您必须使用此脚本处理文件,因此需要更多
资源不仅仅是普通的下载链接。不过,这取决于您的需求。如果您认为这些文件需要更高的安全性。假设只有经过身份验证的用户才能下载该文件,并且只有该文件属于他。然后你需要验证这些。在这种情况下,您需要在问题中输入的代码。如果您的文件向公众开放,那么您可以显示该文件的直接链接,可能会暂时将它们定位在公共的某个地方。
2 我可以建议您使用两种方法来执行此操作。
方法1:
您需要javascript支持才能方便地执行此类需求。假设您需要在可以下载的页面上显示一些 HTML。您可以使用所需的 HTML 创建一个页面,并且可以放置一个下载按钮。
并且您可以保留隐藏的 iframe 来处理下载。
假设您的 PHP 下载页面是 download.php。
然后你就可以有一个像这样的javascript函数。
方法2:
除了上述方法之外,您还可以使用META Refresh。
你也可以用它来显示 HTML。
1 Since you have to process the file with this script it require more
resources than just normal download link. However this is depend on your needs. If you think these files need more security. Let's say only authenticated users can download the file and only the file belongs to him. Then you need to validate those. In such situation you need the code you have put in your question. If your files are open to the public then you can display direct link to the file may be locating them somewhere in public temporarily.
2 I can suggest you two methods to perform this.
Method 1 :
You need javascript support to perform this kind of requirement in handy way. Assume you need to display some HTML on the page where the download is possible. You can create a page with the HTML you want and you can put a download button.
<input type="button" name="cmdDownload" id="cmdDownload" value="Download" onclick="downloadFile('<?php echo $pathToTheFile; ?>');" />
And you can keep hidden iframe to process the download.
Assume your PHP download page is download.php.
Then you can have a javascript function like this.
Method 2:
Other than above method you can use META Refresh as well.
You can have HTML display with this too.
(或 javascript)重定向以转发用户到实际的下载网址。
<meta>
(or javascript) redirect to forward the user to the actual download url.如果输出缓冲处于活动状态,使用 PHP 提供文件将消耗大量内存,并且性能将比 apache 提供的常规文件差很多。
因此,要在已经给出的答案中添加一些内容:
有人说过,但强调得不够:要包含
Content-Length
标头,因为它将允许下载者测量进度。If output buffering is active, serving a file using PHP will consume a lot of memory, and performance will be much worse than in the case of regular files served by apache.
So to add something to the answers already given:
ob_end_flush()
, so the file contents will not be cached in memory before starting outputreadfile()
might stream a file from disk to the web more efficiently than your loopIt has been said but cannot be stressed enough: do include the
Content-Length
header, since it will allow the downloader to measure progress.