Downloading Files Problem

Discussion in 'PHP' started by Weirfire, Aug 29, 2007.

  1. #1
    I've got this bit of code here which allows me to force the script to download pdf files, images and any other sort of file which the browser may try and open itself.

    	$location = "pdfs/" . $filename;
    
    		//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($location));
    
    		@readfile($location);
    PHP:
    My problem is that sometimes it works and other times when going to open the PDF file it displays an error that the file is corrupted. Based on the code above is there anything which could be causing this which is under my control to fix or could it be caused by the user's machine?

    If you need any more information than what I've given you please don't hesitate to ask me. Thanks in advance.
     
    Weirfire, Aug 29, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Try setting this before outputting the file, to make sure no other content is sent.
    
    ob_clean();
    
    PHP:
    Add double quotes around the file name.
    
    header('Content-Disposition: attachment; filename="' . $filename .'"');
    
    PHP:
    Make sure the file name has no special characters.


    Send the appropriate content type for PDFs
    
    header('Content-Type: application/pdf');
    
    PHP:
    (And remove the application/octet-stream thing)


    And if this still doesn't work, try without the Content-Description. I don't think it's causing any troubles, but it's worth a try.
     
    nico_swd, Aug 29, 2007 IP
  3. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #3
    It still works for me but it's still not working on my clients machine. :confused:

    Thanks for all your comments.

    Is there anything else that would bring up an error for some people and not for others?



     
    Weirfire, Aug 30, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Okay, further things.

    -Remove the post-check=0, (Don't mess up the commas after)
    -Add exit(); after readfile(), to make sure no other output is sent to the browser. (Even though the file size header should tell the browser to stop reading after the size is reached).

    Also, IE requires the output compression to be disabled.
    
    if (ini_get('zlib.output_compression'))
    {
        ini_set('zlib.output_compression', 'Off');
    }
    
    PHP:
    Do the file names contain spaces? Try replacing them with underscores or something.

    On which browser doesn't it work? IE?
     
    nico_swd, Aug 30, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    And if you do just:
    
    header('Content-type: application/pdf');
    header('Content-Disposition: attachment; filename="'. $filename .'"');
    readfile($location);
    exit();
    
    PHP:
    Without any other headers... does that work?
     
    nico_swd, Aug 30, 2007 IP
  6. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #6
    I'll try that shortly. We have found that the browser version which the problems transpire is IE6. It works in IE7 and FF for OS X. Other browsers have not been tested.
     
    Weirfire, Aug 30, 2007 IP