开发者

PHP (or Flash) file download - set headers then redirect

I would like to do this, without the "readfile()" part:

header("Content-type: application/force-download"); 
header("Content-Transfer-Encoding: Binary"); 
header("Content-length: ".filesize($file)); 
header("Content-disposition: attachment; filename="".basename($file)."""); 
readfile("$file"); 

Basically I am trying to forward a download to another URL but at the same time I need to be able to set headers like "filename".

The readfile($开发者_C百科file) function will not do it for me, because the the $file is not stored on the same server as the PHP script (not even the same datacenter)

Is this even possible ? If not via PHP, maybe FLASH could do this ?


The only way to do so with PHP is for PHP to first download the file from the remote server, and then send it to the user. You can actually use readfile for this, as it supports fopen wrappers, as long as the option is enabled on your server.


If you really wish to get the file to download in this way via PHP you can use

header("Content-disposition: attachment; filename=myfile.jpg");
  echo file_get_contents("http://host.tld/path/to/myfile.jpg")

Quoted from Byron Whitlock on another SO question here. He explains that this code will put the file in temporary storage, so the download and deletion of the original file will be dealt with automatically.

If you don't have to use PHP, you could use a hidden iFrame on the page to download the file

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜