Sending a file to the browser as an attachment

How to send a file to the browser as an attachment if the specified file is located on a third-party server (without pre-downloading or streaming)?

+7
source share
5 answers

From another server without downloading to your server:

header('Location: http://thirdparty.com/file.ext'); 

Without downloading the file locally, you do not have authorization on the external server, so you must tell the browser what to do, and thus the redirect header, it will tell the server to go directly to the specified url, loading the download.

From your server you will do:

 if (file_exists($file)) { if(false !== ($handler = fopen($file, 'r'))) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); //Remove //Send the content in chunks while(false !== ($chunk = fread($handler,4096))) { echo $chunk; } } exit; } echo "<h1>Content error</h1><p>The file does not exist!</p>"; 

Taken from another question I answered

+14
source

http://php.net/manual/en/function.header.php#example-3655

If you want the user to be asked to save the data that you are sending, for example, a generated PDF file, you can use the "Content-Disposition" header to provide the recommended file name and make the browser display a save dialog.

 <?php // We'll be outputting a PDF header('Content-type: application/pdf'); // It will be called downloaded.pdf header('Content-Disposition: attachment; filename="downloaded.pdf"'); // The PDF source is in original.pdf readfile('original.pdf'); ?> 
+4
source

As others have said, you should set the Content-disposition header of the response to the file download request. However, you said the file on a third-party server. If you do not control how this server sends this file, you cannot change the way the browser receives it from this server.

However, you can proxy the file so that your server downloads the file and then transfers it to the client with the appropriate headers. This means that you doubled your bandwidth bill, however, since you have to download the file at the same time and download it at the same time.

0
source

Just a note for those who encounter problems with names containing spaces (for example, "test test.pdf").

In the examples (99% of the time) you can find header ('Content-Disposition: attachment; filename ='. Basename ($ file));

but the correct way to set the file name is quoting it (double quote): header ('Content-Disposition: attachment; filename = "'. basename ($ file). '"');

Some browsers may work without quotes, but, of course, Firefox and, as Mozilla explains, the quote with the file name in the content is in accordance with the RFC http://kb.mozillazine.org/Filenames_with_spaces_are_truncated_upon_download

0
source

If you want to provide files that are regularly processed by the browser (images, html, ...), you must add a header to change the MIME type using something like:

header("Content-Type: application/force-download; name=filename");

If you do not have access to a third-party server, you have no choice but to upload the file yourself to provide it to the user by adding a header

-one
source

All Articles