2013-01-11 76 views
0

http://www.mysite.comhttp://www.anothersiter.com网站。有链接到一些文件http://www.anothersite.comPHP更改下载位置的技巧

http://www.anothersite.com/file1 
http://www.anothersite.com/file2 
... 
http://www.anothersite.com/fileN 

我想将上面的网站访问者对http://www.mysite.com链接可以下载。但我希望它们被下载,好像它们来自http://www.mysite.com而不是http://www.anothersite.com。是否有可能在PHP中实现这样的场景,以及如果可能的话如何?

+0

这取决于你是什么意思的“下载好像他们来自'http:// www.mysite.com'”这深入到一些潜在的安全漏洞,明显被浏览器阻止。 – crush

+0

是的,您将通过某种类型的代理脚本通过您的服务器下载它们。 – datasage

+0

所以你想代理请求到另一个网站的内容? –

回答

2

您可以创建一个简单的下载脚本,使用file_get_contents通过您的服务器下载该文件。不过,您的php.ini需要启用allow_url_fopen。但代码可以非常简单。例如,要下载一个名为downloaded.pdf PDF文件,您可以使用此:

header('Content-type: application/pdf'); 
header('Content-Disposition: attachment; filename="downloaded.pdf"'); 
echo file_get_contents('http://www.coolermaster-usa.com/upload/download/85/files/product_sheet.pdf'); 

因为它是从您的域下载这将检索的产品表:

Local download

-

更新

在MP3下载的情况下,您可以考虑使用Content-Disposition: inline,而只是将mp3传输到浏览器。像这样:

header('Content-type: audio/mpeg'); 
header('Content-Disposition: inline; filename="somefile.mp3"'); 
echo file_get_contents('http://example.com/somefile.mp3'); 

这样文件就会在线播放。然后,人们可以随时按Ctrl + S下载,或者只是在没有永久保存的情况下收听(在会话期间在临时互联网文件中)。

+0

更好地将'file_get_contents()'输出保存到'$ var'中,并在'echo $ var'之前执行'header(“Content-Length:”.strlen($ var));'以便浏览器成为能够跟踪下载进度。 –

+0

我看到这种方式比直接下载慢。是否因为localhost下载它? – torayeff

+0

是的,您的服务器充当首先存储文件的代理主机。如果你打算提供大文件,使用'fsockopen'来设置通过套接字的HTTP连接可能会更快。 – Oldskool