I’m working on a project that has file uploads. These files are given a random name and placed out side of the web directory.
I then have a script that retrieves this image based on parameters passed in the URL which works absolutely fine.
However, I made an array of the headers I wanted to pass to the browser and then looped through these with a foreach loop and they didn’t seem to be setting anything. But, if I set them manually with the header function they work! Bizarre!
Now, I do not mind using the header function instead of looping through them, but, is there a specific reason that this doesn’t work?
I also use the above method to store all my headers in an array and then process them upon delivering the content to the browser, but, I cannot say for certain if they are actually being passed, seeing the aforementioned issue!
Snippet of my code:
// Expiry date in seconds, in this instance a year
$expiry_date= ( 60 * 60 * 24 * 365 );
// Our headers
$img_headers= array(
'Content-Type : ' . $mime,
'Cache-Control: no-cache, must-revalidate',
//'Cache-control: max-age=' . $expiry_date,
'Expires:' . gmdate( DATE_RFC1123, time() + $expiry_date ),
'Pragma: ',
'Last-Modified: ' . gmdate( gmdate( DATE_RFC1123 ), filemtime( $image_path ) ),
'Content-Length: ' . ( string ) filesize( $image_path ),
'Content-Disposition: inline; filename="' . $image_name . '"'
);
/*header( 'Content-Type: ' . $mime, TRUE );
header( 'Cache-Control: no-cache, must-revalidate', TRUE );
header( 'Expires: ' . gmdate( DATE_RFC1123, time() + $expiry_date ) );
header( 'Pragma: ', TRUE );
header( 'Last-Modified: ' . gmdate( gmdate( DATE_RFC1123 ), filemtime( $image_path ) ), TRUE );
header( 'Content-Length: ' . ( string ) filesize( $image_path ), TRUE );*/
// Loop through and set up our headers!
foreach( $img_headers as $new_header )
{
header( $new_header, TRUE );
}
// Read our image and end the rest of the file execution
return die( readfile( $image_path ) );
Anything there that looks out of place?
Thanks in advance!
Those two methods should be identical. Maybe the extra space in “Content-Type :” is causing the problem?