PHP Download file - Filename

Discussion in 'PHP' started by Weirfire, Sep 19, 2006.

  1. #1
    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:
     
    Weirfire, Sep 19, 2006 IP
  2. Big 'G'

    Big 'G' Member

    Messages:
    89
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    48
    #2
    header("Content-Disposition: attachment; filename=".$filename);
    try
    header("Content-Disposition: attachment; filename=$filename");
     
    Big 'G', Sep 19, 2006 IP
  3. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #3
    Thanks Big G. I'll let you know if that works tomorrow when I get into the office :)
     
    Weirfire, Sep 19, 2006 IP
  4. vishwaa

    vishwaa Well-Known Member

    Messages:
    271
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #4
    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:
     
    vishwaa, Sep 20, 2006 IP
  5. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #5
    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.
     
    PinoyIto, Sep 20, 2006 IP