The Little Guy Posted March 20, 2007 Share Posted March 20, 2007 I found this code to force a download, but when I try to use the function with something like this: force_download('http://us3.php.net/images/php.gif', 'http://us3.php.net/images/php.gif'); It downloads, but the file it downloads is empty. Any way to improve this code? or is there a better code? <?php function force_download ($data, $name, $mimetype='', $filesize=false) { // File size not set? if ($filesize == false OR !is_numeric($filesize)) { $filesize = strlen($data); } // Mimetype not set? if (empty($mimetype)) { $mimetype = 'application/octet-stream'; } // Make sure there's not anything else left ob_clean_all(); // Start sending headers header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); // required for certain browsers header("Content-Transfer-Encoding: binary"); header("Content-Type: " . $mimetype); header("Content-Length: " . $filesize); header("Content-Disposition: attachment; filename=\"" . $name . "\";" ); // Send data echo $data; die(); } function ob_clean_all () { $ob_active = ob_get_length () !== false; while($ob_active) { ob_end_clean(); $ob_active = ob_get_length () !== false; } return true; } ?> Link to comment https://forums.phpfreaks.com/topic/43534-force-download/ Share on other sites More sharing options...
per1os Posted March 20, 2007 Share Posted March 20, 2007 Wouldn't it be more like this: force_download('http://us3.php.net/images/php.gif', 'php.gif'); ??? Link to comment https://forums.phpfreaks.com/topic/43534-force-download/#findComment-211404 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.