[UPHPU] RE: PHP indirect download

Kenneth Burgener email at kennethburgener.com
Thu Feb 5 22:39:27 MST 2004


Wow.  It's amazing what you can stumble upon when searching php.net.

http://www.php.net/manual/en/function.header.php

If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the Content-Disposition header to supply a recommended filename and force the browser to display the 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');
?>  


-----Original Message-----
From: Kenneth Burgener [mailto:email at kennethburgener.com]
Sent: Tuesday, February 03, 2004 1:33 PM
To: Jonathan Gale; Kenneth Burgener
Cc: list at uphpu.org
Subject: RE: [UPHPU] RE: PHP indirect download


Sweet!  Thanks for the quick response.

-----Original Message-----
From: Jonathan Gale [mailto:jgale at j2.net]
Sent: Tuesday, February 03, 2004 1:27 PM
To: Kenneth Burgener
Cc: list at uphpu.org
Subject: Re: [UPHPU] RE: PHP indirect download


Kenneth,

It's a pretty trivial task to have a PHP script serve up a file from 
another (non HTTP accessible location).  Something like this would work:

<?php
$filepath = '/path/to/files';
$file = $_GET['file'];
Header('Content-type: application/octet-stream');
readfile("$filepath/$file");
?>

The trick however is sending the correct headers so the download works 
correctly in all browsers.

This header set will generally work:

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; 
filename=".basename($filename).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));

Some good reference URLs:

http://us4.php.net/manual/sv/function.readfile.php
http://us4.php.net/manual/sv/function.fpassthru.php

Good luck.

Jon

Kenneth Burgener wrote:

> I would like to make it so that users do not have a link to directly access a downloadable file.  I want the script to push the file.  I see websites doing this all the time.  Any suggestions?
> 
> Normal download:
> http://mydomain.com/file.zip
> 
> What they look like:
> http://mydomain.com/download.php?file=QWERASDFASDFASDF
> 
> Thanks in advance.
> 
> 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: list-unsubscribe at uphpu.org
> For additional commands, e-mail: list-help at uphpu.org
> 
> 










More information about the UPHPU mailing list