I'm writing a script to download files that are not stored within the htdocs area of my site. I can display the files, and seem to download them - but then when i try and open them on my computer - they won't open any more! I'm trying it with jpg images at the moment..

So there is obviously something wrong with my download script! I'm fairly new to PHP. The function i'm using to download the file is give below

function DownloadFile($filename) 
{ 
    // Verify filename 
    if (empty($filename) || !file_exists($filename)) 
    { 
        return FALSE; 
    } 
    // Create download file name to be displayed to user 
    $saveasname = basename($filename); 
    header('Content-Disposition: attachment; filename="'.$saveasname.'"'); 
    Header("Content-Length: ".filesize($filename));
	Header("Content-Type: application/x-download");
	Header("Content-Disposition: attachment; filename=\"".rawurlencode($saveasname)."\"");
    // Output file 
    readfile($filename); 
    // Download successfully started 
    die();
} 

Help, please!

    Could it be the die(); command

    Try using return TRUE;

      You have two entries for Content-Disposition:

      Try the first one...it's worked with another script I've used.

      Also, try changing the content type to Content-type: application/octetstream);

      This code works for me (different variable names naturally)

      header('Content-type: application/octetstream');
      header('Content-Length: ' . filesize("./storage/".$validdownload[0]));
      header('Content-Disposition: attachment; filename="'.$validdownload[1].'"');
      readfile("./storage/".$validdownload[0]);
      

      no die(); or return is used in this case

        Tried that. Still doesn't work :bemused:

        Also I opened up the jpeg file in notepad - and it seems to be adding all the html header stuff from my index.php file into the top of the jpeg file - when downloading.

        And if i use any 'echo' commands for debugging - it inputs these too?!

        any idea?

        Mike

          I just tried your original script extract on my local testing server and my public webspace and it worked fine for files within my htdocs folder but not those one level up.

          I think it's got something to do with the fact the file you are trying to access it outside your htdocs folder.........the server might not be able to find it properly.

          How are you referring to it........are you using a relative path???

            ClarkF1 wrote:

            I just tried your original script extract on my local testing server and my public webspace and it worked fine for files within my htdocs folder but not those one level up.

            I think it's got something to do with the fact the file you are trying to access it outside your htdocs folder.........the server might not be able to find it properly.

            How are you referring to it........are you using a relative path???

            Yes I am.. I've got it to work now - just by moving the PHP function to the top of the page (before the HTML)..

            Strange problem though...

              Try use this function:

              function send_file($file)
              {
              	if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']))
              	{
              		// IE Bug in download name workaround
              		ini_set( 'zlib.output_compression','Off' );
              	} 
              	header("Cache-Control:");
              	header("Cache-Control: public"); 
              	header ('Content-type: ' . mime_content_type($file));
              	header ('Content-Disposition: attachment; filename="'.basename($file).'"');
              	header("Accept-Ranges: bytes");
              	$size = filesize($file);
              	header("Content-Length: ".$size);
              	if ($fp=fopen($file,"rb"))
              	{
              		while(!feof($fp) and (connection_status()==0))
              		{
              		//reset time limit for big files
              			set_time_limit(0);
              			print(fread($fp,1024*8));
              			flush();
              		}
              		$status = (connection_status()==0);
              		fclose($fp); 
              	}
              	return($status);
              }
              

              THe $file variable should be the FULL path to that file:
              e.g. for win: C:/path/to/the/file.exe

                Glad you got it working.....obviously something to do with the order the headers were sent as it had already outputted the html code

                  I am trying to use rmarescu's download script to download a .csv file, but all it does it display the contents of the file in my browser instead of downloading the file.

                  Why would the script do that?

                    You system might be set up so it sees it as a text file and the browser opens it as such.

                      Ah ok, makes sense. Is there any way that one can force the browser to download the file without treating it as a text file? I have some seriously backward users who feel that the "Save Target As..." steps are too complicated. LOL

                        Change

                        header ('Content-type: ' . mime_content_type($file));

                        to

                        header ('Content-type: application/octetstream');

                        The script posted by rmarescu finds the content-type of the file and downloads it accordingly. I find this downloads anything as if it was an attachment.

                          Write a Reply...