Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts

    readfile in php isn't downloading zip file

    All,
    I've got the following code to force a file download for a zipfile that I just created:
    PHP Code:
        $zip_name 'download.zip';
        
    $result create_zip($other_files_to_zip,$zip_name,true);
        if(
    $result){
        echo 
    "The filesize is: ".filesize($zip_name);
        
    header('Content-Type: application/zip');
        
    header('Content-disposition: attachment; filename=filename.zip');
        
    header('Content-Length: ' filesize($zip_name));
        
    readfile($zip_name);
        } 
    When I echo out the filesize it shows a correct size and it prompts me to download the filename.zip which is ok since it's just a filename but then when I save the document and try and open Windows says that Windows cannot open the folder because the folder is invalid.

    Any ideas on how to resolve an issue like this?

    Thanks in advance!

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,222
    Thanks
    15
    Thanked 253 Times in 253 Posts
    Having had to figure this out myself, I tell you that when you make a download.php it has to be in a separate file, with no spaces before or after the php tags and called by a simple href link to it. You can pretty much put what you want in it, run whatever code, but it has to be made that way.

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    Are you sure? It seems like you have to be able to force a download because I've done it for some excel exports that I run. I'm just not sure why it's not working for my zip file. This is the first time using readfile though...

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,364
    Thanks
    61
    Thanked 530 Times in 517 Posts
    Quote Originally Posted by treeleaf20 View Post
    Are you sure?
    Yes.

    Your download.php file must contain no spaces before the <?php tag and no spaces after the ?> tag otherwise it will corrupt the filestream output. Again, you have to call the download.php file via a link and not as part of a webpage. A webpage script puts out html - so you can't output a filestream in the middle of that can you - the browser won't have any idea what to do with it.

    Additionally, you need to test one small file being downloaded - say 100Kb in size. Download it and open it in notepad++ or similar and look in there for any php error messages.

    Also check the MD5 hashes before and after download. In php use md5_file() to generate the hash and log it to a DB table so that you can get to it without affecting the filestream. Once downloaded, compare that hash with one you generate locally on the file on your system. If they differ then you have a problem somewhere.

    I've done a lot of file download coding in the past
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,364
    Thanks
    61
    Thanked 530 Times in 517 Posts
    Have you had any luck getting this working?
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #6
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    Yeah but not how I wanted it to. Like you suggested I return a link using jQuery and then allow the user to click on a link to download the file. I pass the filename to my download.php file and then it works fine.

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,364
    Thanks
    61
    Thanked 530 Times in 517 Posts
    Glad you've got it working
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •