I have a piece of code which allows people to download a file when they click a link even if it is an image file. However when they go to download it, it always asks the user to save it as "download.[tag]" instead of saving it as the filename it already has. If anyone knows how to sort this so that it saves it with the existing filename please let me know. //Begin writing headers 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: application/octet-stream"); header("Content-Disposition: attachment; filename=".$filename); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($base_file . $script_file . $filename)); PHP:
header("Content-Disposition: attachment; filename=".$filename); try header("Content-Disposition: attachment; filename=$filename");
I guess that cache control and pragma should be private. May be I am wrong. Here is a code that works fine for me in all browsers. function downloadFile ($file, $mimetype,$outputFileName) { $status = 0; if (($file != NULL) && file_exists($file)) { if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT'])) { ini_set( 'zlib.output_compression','Off' ); } header ('Content-type: ' . $mimetype); header ('Content-Disposition: attachment; filename='.$outputFileName); header ('Expires: '.gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y"))).' GMT'); header ('Accept-Ranges: bytes'); header('Cache-control: private'); header('Pragma: private'); $size = filesize($file); if(isset($_SERVER['HTTP_RANGE'])) { list($a, $range) = explode("=",$_SERVER['HTTP_RANGE']); str_replace($range, "-", $range); $size2 = $size-1; $new_length = $size2-$range; header("HTTP/1.1 206 Partial Content"); header("Content-Length: $new_length"); header("Content-Range: bytes $range$size2/$size"); } else { $size2=$size-1; header("Content-Range: bytes 0-$size2/$size"); header("Content-Length: ".$size); } if ($file = fopen($file, 'r')) { while(!feof($file) and (connection_status()==0)) { print(fread($file, 1024*8)); flush(); } $status = (connection_status() == 0); fclose($file); } } return($status); } PHP:
You may try this one.... ob_start(); $dir = ""; //directory where the file is store. example : /home/lexor/public_html/dir/ $file_name = ""; //File name $file = $dir.$file_name; $file_type = $_FILES['$file']['type']; header("Content-Type: ".$file_type); header("Content-Disposition: attachment; filename=".$file_name); readfile($file); ob_end_flush(); Code (markup): This code work fine in my server, you may try it.