NYCPHP Meetup

NYPHP.org

[nycphp-talk] NEW PHundamentals Question - Headers & Downloads

Dan Cech dcech at phpwerx.net
Wed Oct 13 14:21:38 EDT 2004


You may wish to use specific headers to force proxy servers not to cache 
your files.  Examples of this would be if you wish the files to be 
accessible to only authenticated users, or if you are regenerating 
content regularly (daily reports, etc) and you want to ensure that the 
user has the most recent version.

I have attached an example of a function used to set appropriate 
cache-control headers for preventing proxy/client caching.

The best source of information on this subject is of course RFC2616 
(http://www.faqs.org/rfcs/rfc2616.html), and any recommendations on the 
subject of headers should refer back to this document for justification.

I apologize for the capitalization errors in my previous email, the 
example should have read:

$attach = array(
   'filename' => 'myfile.pdf',
   'mimetype' => 'application/pdf',
   'content'  => 'pdf here'
);

header('Content-Disposition: attachment; filename='. $attach['filename']);
header('Content-Length: '. strlen($attach['content']));
header('Content-Type: '. $attach['mimetype']);
echo $attach['content'];
exit;

You could also add an MD5 header to allow user agents to check the 
integrity of the downloaded file:

header('Content-MD5: '. md5($attach['content']));

For a further discussion of the Content-Disposition header see RFC2183 
(http://www.faqs.org/rfcs/rfc2183.html).

Dan

Jeff Siegel wrote:
> Dan,
> 
> If the cache control is relevant to the issue at hand - forcing browser 
> downloads - then it would make sense to explore that too.
> 
> Jeff
> 
> Dan Cech wrote:
> 
>> Jeff Siegel - PHundamentals wrote:
>>
>>> For the next PHundamentals article, we are exploring best practices 
>>> in the use of headers.  Over the next few weeks, we'll be asking 
>>> about the various ways in which headers are used.  This week's 
>>> question (see below) concerns the use of headers which force downloads.
>>
>>
>>
>> I have used this combination of headers with pretty good success:
>>
>> Given an array containing the details of the file like:
>>
>> $attach = array(
>>   'filename' => 'myfile.pdf',
>>   'mimetype' => 'application/pdf',
>>   'content'  => 'pdf here'
>> );
>>
>> header('Content-Disposition: attachment; filename='. 
>> $attach['filename']);
>> header('Content-length: '. strlen($attach['content']));
>> header('Content-type: '. $attach['mimetype']);
>> echo $attach['content'];
>> exit;
>>
>> The Content-Disposition header tells the browser to open the download 
>> dialog, and lets it know the correct filename/extension.
>>
>> The Content-length header allows the browser to display a meaningful 
>> progress bar.
>>
>> The Content-type header lets the browser correctly display an 
>> indication of the file type to the user (PDF,XLS,EXE etc).  In most 
>> browsers it will also influence the default action (open vs download).
>>
>> For some files (pdf mostly) it can be a good idea to use a different 
>> Content-Disposition header, namely:
>>
>> header('Content-Disposition: inline; filename='. $attach['filename']);
>>
>> In most browsers this will result in the pdf file being opened within 
>> the browser window.
>>
>> I have a bunch more information regarding the use of headers for cache 
>> control...but that wasn't what you were asking was it ;)
>>
>> Dan Cech
>> _______________________________________________
>> New York PHP Talk
>> Supporting AMP Technology (Apache/MySQL/PHP)
>> http://lists.nyphp.org/mailman/listinfo/talk
>> http://www.newyorkphp.org
>>
> _______________________________________________
> New York PHP Talk
> Supporting AMP Technology (Apache/MySQL/PHP)
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.newyorkphp.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20041013/db34cd6d/attachment.html>


More information about the talk mailing list