nuttycoder Posted January 9, 2009 Share Posted January 9, 2009 Hi am tying to downlload images from my site using headers here the code am using: $target = 'assets/images/site/logo.png'; $type = 'image/png'; $name = 'logo.png'; $size = filesize('assets/images/site/logo.png'); //offer file for download using headser header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Type: $type"); header("Content-Disposition: attachment; filename=$name"); header("Content-Transfer-Encoding: binary"); header("Content-Length: $size"); readfile($target); When I run the file I download the image but the image is corrupt anyone know where I'm going wrong? Link to comment https://forums.phpfreaks.com/topic/140217-solved-help-with-download-script-using-headers/ Share on other sites More sharing options...
flyhoney Posted January 9, 2009 Share Posted January 9, 2009 Try this (take from http://us.php.net/readfile) <?php $file = 'assets/images/site/logo.png'; if (file_exists($file)) { 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)); ob_clean(); flush(); readfile($file); exit; } ?> Link to comment https://forums.phpfreaks.com/topic/140217-solved-help-with-download-script-using-headers/#findComment-733691 Share on other sites More sharing options...
nuttycoder Posted January 9, 2009 Author Share Posted January 9, 2009 thanks that works perfectly! Link to comment https://forums.phpfreaks.com/topic/140217-solved-help-with-download-script-using-headers/#findComment-733696 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.