Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP更改下载位置技巧_Php - Fatal编程技术网

PHP更改下载位置技巧

PHP更改下载位置技巧,php,Php,有http://www.mysite.com和http://www.anothersiter.com站点。http://www.anothersite.com: http://www.anothersite.com/file1 http://www.anothersite.com/file2 ... http://www.anothersite.com/fileN 我想在http://www.mysite.com供网站访问者下载。但我希望它们被下载时就像它们来自http://www.mysit

http://www.mysite.com
http://www.anothersiter.com
站点。
http://www.anothersite.com

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中实现这种情况?如果有可能,如何实现?

您可以创建一个简单的下载脚本,使用
file\u get\u contents
通过服务器下载文件。您的php.ini需要启用
allow\u url\u fopen
才能工作。但是代码可以非常简单。例如,要下载名为
download.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');
这将检索从您的域下载的产品表:

--

更新

在MP3下载的情况下,你可以考虑使用<代码>内容处理:内联,而只是将MP3流到浏览器。像这样:

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

这样,文件将在线播放。然后,人们可以始终按CTRL+S来下载它,或者只听它而不将它永久保存在某个地方(在会话期间仅保存在临时internet文件中)。

这取决于您所说的“下载时就像他们来自
http://www.mysite.com
”这将深入研究浏览器明确阻止的一些潜在安全漏洞。是的,您将通过某种类型的代理脚本通过服务器下载这些漏洞。因此,您想将请求代理到另一个网站的内容?假设有。此链接发布于。通常,当您单击它时,它会从中下载,文件名为file1,但我希望用户看到,如果该文件来自,请准备支付用户下载带宽的两倍。最好将
file\u get\u contents()
输出保存到
$var
,并执行
标题(“内容长度:.strlen($var))
在您
echo$var
之前,浏览器将能够跟踪下载进度。我认为这种方式比直接下载慢。是因为localhost下载了它吗?是的,您的服务器充当代理主机,首先存储文件。如果您计划提供大文件,使用类似
fsockopen
的方法通过套接字设置HTTP连接可能会更快。@Oldskool我提供的mp3音频文件大小为4-10 mb。有什么建议吗?@torayeff如果是这样的话,我会选择内嵌mp3。请参阅我答案的更新。