PHP: force an image download from above directory root?
I'm attempting to force a download of an image that is in a directory above my website 开发者_高级运维root. The download happens ok, and the correct filename is saved. However, the end-file is not a valid image and will not open or display properly. Here's my code:
$photograph = new ViewPhotograph($photograph_id);
$photograph->setPhotographVars();
$file = $photograph->getPath('small');
$filename = '1.jpg';
header('Content-Description: File Transfer');
header('Content-Type: image/jpg');
header('Content-Disposition: attachment; filename=' . $filename);
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));
ob_clean();
flush();
readfile($file);
exit;
do a ob_start before calling ob_clean()
once something is outputed by using echo or similar, you can't get rid of this, except, when you start a buffer before
Perhaps there's a PHP warning corrupting the stream.
Comment out the headers and see if you see a warning. If you do, fix it. (note that you shouldn't have display_errors
turned on production servers).
精彩评论