使用PHP提供文件下载 [英] Serving a file download using PHP

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

问题描述

第一个PHP项目,我被卡住了!

我希望允许用户点击按钮或链接并下载文件。

First PHP project and I'm stuck!
I wish to allow users to click a button or a link and download a file.

但是,我的PHP必须首先执行一些任务,选择正确的文件,将数据库中的下载事件记录下来等等。我可以这样做,但是如何将文件作为响应发送给用户的点击?

However, my PHP must first perform some tasks, choose the right file, record the download event in a db etc. This I can do, but how do I then send the file as a response to the user's click?

感谢任何帮助。

推荐答案

由于@Sarfraz建议,执行您需要执行的任务,将 Content-Type 头发送到浏览器,然后将文件的内容发布到浏览器。然后,浏览器将根据用户设置进行操作,一般情况是:a)打开并显示文件,或b)将文件保存到磁盘。

As @Sarfraz suggests, you can, after doing the tasks you need to do, send a Content-Type header to the browser and then spew out the contents of the file to the browser. The browser will then perform according to user settings, which in general will be either a) open and display the file or b) save the file to disk.

要强制将文件保存到磁盘而不是显示在浏览器中,常用的方法是指定一个mime类型的 Content-Type:application / octet-stream 。还可以使用标题 Content-Disposition:attachment来指定附件文件名; filename = foobar.baz

If you want to force the file to be saved to disk instead of displayed in the browser, a commonly used method is to specify a mime-type of Content-Type: application/octet-stream. It is possible to specify an attachment filename as well, with the header Content-Disposition: attachment; filename=foobar.baz.

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

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