2

I've been using SO for a long time but finally had a question I couldn't find an answer for. I want to use my site to redirect a user to a file that is hosted on another server. At first I thought "this should be easy":

header("Location: $url")

However, the file hosted on the other server just has a uuid as a filename, so I want to use headers to pass an appropriate mimetype and filename (which I will know). This I can do with the following code:

<?php
$filename = $_REQUEST["name"];
$filesize = $_REQUEST["size"];
$mimetype = $_REQUEST["mime"];
$url = $_REQUEST["url"];

// used to test
$filename = "example.txt";
$filesize = "2966";
$mimetype = "text/plain";
$url = "http://example.com";

header('Content-Type: ' . $mimetype); 
header("Content-length: " . ($filesize + 0)); 
header('Content-Disposition: attachment; filename="' . $filename . '"'); 

// this just redirects to the page, ignoring the Content- headers
//header('Location: ' . $url);

// this works, but I suspect that it uses my server's bandwidth
readfile($url);
exit();

?>

The method I've posted above does indeed present the user with a download, but doesn't it pass the download through my own servers? I could potentially be downloading a lot of files with this and I'd rather not use my own bandwidth for this if I don't have to. I realize this may not be possible, but I appreciate any insight.

MalcolmOcean
  • 2,807
  • 2
  • 29
  • 38
  • if your setting the headers from your server then the content has to come the same way so its going to have to use your bandwidth –  Oct 30 '11 at 22:15
  • 2
    It is precisely as you suspect. The `readfile()` call consumes bandwidth on your end, and imposing HTTP readers on external resources with a redirect is impossible. Unless you find an external service which does that (and provides bandwidth), there are no workarounds here. – mario Oct 30 '11 at 22:20
  • *sigh* I thought as much. Thanks guys. – MalcolmOcean Oct 30 '11 at 22:28
  • Is there something I should do to mark this question as resolved? – MalcolmOcean Oct 30 '11 at 22:28
  • 1
    Delete it is all I can think of – comu Oct 30 '11 at 22:36
  • 1
    You can answer it yourself ;) – Shad Oct 30 '11 at 22:38
  • @mario 's comment should be turned into an answer. No reason to delete this question as it fits the requirements. – pixeline Oct 30 '11 at 22:43

2 Answers2

2
$file=fopen('http://example.com/example.txt','r');
header("Content-Type:text/plain");
header("Content-Disposition: attachment; filename="example.txt");
fpassthru($file);

Works for me for downloading MP4 files.

bob_flem
  • 74
  • 6
  • Thats the correct answer. Almost none traffic usage in the main server while passing streamable download to the user – Tautvydas Feb 19 '21 at 16:38
0

The short answer is, this just isn't possible; a browser will only look at the headers from the server delivering the file.

You either need to proxy the request via your server (which without caching doubles the bandwidth), or talk to the owner of the site files are downloaded from about supporting your requirements.

Phil Lello
  • 8,377
  • 2
  • 25
  • 34
  • Thanks. Fortunately I happen to have a connection to the owner of the site, so I may be able to actually get something done here. – MalcolmOcean Oct 31 '11 at 02:29