PHP 重定向 +强制下载
我正在创建一个 google-chrome 应用程序,它将从八位字节流下载歌曲,但是由于 JavaScript 限制,我无法创建“下载”按钮。用户必须右键单击并选择文件另存为。我希望创建一个 php 页面,它将浏览器重定向到八位字节流的位置,该位置将位于不同的站点上,然后在那里创建一个下载对话框。我知道这里可能存在安全限制,但是是否可以进行 php 页面重定向并设置其重定向到的页面的内容处置标头?
注意:我无法从服务器获取八位字节流进行保存,因为我使用的主机不支持对外部站点的 php 调用。
I am creating a google-chrome application that will download songs from an octet stream, however due to JavaScript restrictions, I cannot create a "download" button. The user must right click and select save file as. I wish to create a php page that will redirect the browser to the location of the octet stream, which will be on a different site, and then create a download dialog there. I know there are probably security restrictions here, but is it possible to have a php page redirect and set the content disposition headers of that page it is redirecting to?
Note: I cannot get the octet stream from my server to save because the host I am using does not support php calls to external sites.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下载对话框(内容处置标头)必须由其他站点生成。没有办法解决这个问题,因为浏览器只会相信来自同一服务器的标头。如果其他网站没有生成所需的标头,并且您不拥有该网站,则您无能为力。
您可以编写一个 PHP 脚本,该脚本代表用户访问其他站点,首先将八位字节流下载到服务器,然后将其与所需的标头一起发送给用户。换句话说,您的脚本将充当代理服务器。如果有兴趣,请查看
curl
模块。但这会导致你的服务器带宽暴涨,而且还可能出现版权方面的问题。The download dialog (content-disposition header) has to be produced by the other site. There's no way around it, because browsers will only believe headers from the same server. If the other site doesn't produce the desired headers, and if you don't own the other site, there's nothing you can do about it.
You might write a PHP script which accesses the other site on behalf of the user, downloads the octet stream to the server first, and then sends it to the user with the desired headers. In other words, your script would act as a proxy server. Look into the
curl
module if interested. But this will cause your server's bandwidth to skyrocket, and there may also be problems with copyright.