将服务文件托管在另一个服务器上作为下载 [英] Serving file hosted on another server as download

查看:410
本文介绍了将服务文件托管在另一个服务器上作为下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单但关键的问题(对我的应用程序至关重要)



我将有一个文件网址:

  http://a.com/b.jpg 
http://a.com/b.zip
http:// a。 com / b.mp3
<或任何有效的文件>

当用户点击我网站上任何特定文件的下载链接,b.com,用户将看到url为

  http://b.com/?f=1 

我不希望用户看到原始URL,其次,想强制下载文件, p>

我知道我可以使用readfile来实现这一点(请参阅 http://php.net/manual/en/function.readfile.php ),但我不知道filesize和mimetype,我怎么能得到保证文件将正确下载?



请帮助别人

解决方案

我想你可以使用cURL触发对目标网址的HEAD请求。这将使托管目标的Web服务器具有文件的mimetype和内容长度。

  $ url ='http:// www.example.com/path/somefile.ext'; 
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_HEADER,true);
curl_setopt($ ch,CURLOPT_NOBODY,true); // make it一个HEAD请求
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,TRUE);
$ head = curl_exec($ ch);

$ mimeType = curl_getinfo($ ch,CURLINFO_CONTENT_TYPE);
$ size = curl_getinfo($ ch,CURLINFO_CONTENT_LENGTH_DOWNLOAD);
$ path = parse_url($ url,PHP_URL_PATH);
$ filename = substr($ path,strrpos($ path,'/')+ 1);

curl_close($ ch);然后,您可以将这些头文件写回到您的脚本上的HTTP请求:

  header('Content-Type:'。$ mimeType); 
header('Content-Disposition:attachment; filename ='。$ filename。'';');
header('Content-Length:'。$ size);

然后你按照文件内容。

  readfile($ url); 


I have a simple, yet critical question (critical for my application)

I will have a file url as:

http://a.com/b.jpg
http://a.com/b.zip
http://a.com/b.mp3
<or any valid file>

When user will click on download link for any specific file (say b.jpg) on my site i.e., b.com, user will see url as

http://b.com/?f=1

I don't want user to see original URL and secondly, want to force download of file, irrespective of filetype

I know that I can achieve this using readfile (Check Example1 at http://php.net/manual/en/function.readfile.php), but I don't know filesize and mimetype, how can I get assurance that file will be downloaded properly?

Please help guys

解决方案

I suppose you can use cURL to fire off a HEAD request for the target URL. This will let the web server hosting the target the mimetype and content length of the file.

$url = 'http://www.example.com/path/somefile.ext';
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_NOBODY, true); // make it a HEAD request
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
$head = curl_exec($ch);

$mimeType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
$path = parse_url($url, PHP_URL_PATH);
$filename = substr($path, strrpos($path, '/') + 1);

curl_close($ch); 

Then, you can write back these headers to the HTTP request made on your script:

header('Content-Type: '.$mimeType);
header('Content-Disposition: attachment; filename="'.$filename. '";' );
header('Content-Length: '.$size);

And then you follow this up with the file contents.

readfile($url);

这篇关于将服务文件托管在另一个服务器上作为下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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