Results 1 to 15 of 15
  1. #1
    Join Date
    Aug 2002
    Location
    Canada!
    Posts
    159

    corrupted downloads using fpassthru

    PHP Code:
    <?php    header("Content-Disposition: attachment; filename={$this->settings['realname']}");
            
    header("Content-Type: {$this->settings['mimetype']}");
    //header("Content-type: application/octet-stream");
            
    header("Content-Length: {$this->settings['filesize']}");
    //header("Connection: close");
            
    header("Pragma: no-cache");
            
    $fp fopen(/path/to/download,"rb");
    //$fp = fopen(/path/to/download,"r");
    //        sleep(1);
            
    fpassthru($fp);
    //print fread($fp,filesize(/path/to/download));
            
    fclose($fp);
            
    flush();
            exit();    
    ?>
    I'm having a slight problem here... It downloads zip files 2MB or under fine but if you download something 4MB or higher the zip file becomes corrupted.

    I have the following enabled:
    ini_set("memory_limit", "32M");
    ini_set("max_execution_time","0");

    Any help would be great, thanks!

  2. #2
    Join Date
    Aug 2002
    Location
    Canada!
    Posts
    159
    Anyone at all have any ideas?

  3. #3
    Join Date
    Aug 2001
    Posts
    5,068
    Might your PHP installation be running in safe mode? If so max_execution_time wont have effect and this might cause your problems (considering it takes longer than the maximum time (default 30 seconds) to transfer the file).

    Otherwise its not easy to say without seeing an example, would you have some link?
    Sitemeer.com - Is your site up?
    Multi-Location Service Availability Check ● yes, we do HTTPS & IDN!


  4. #4
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,342
    The most efficient thing to do is a loop with maybe 1024657 reading a second.

    Peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  5. #5
    Join Date
    Aug 2001
    Posts
    5,068
    Quote Originally Posted by azizny
    The most efficient thing to do is a loop with maybe 1024657 reading a second.

    Peace,
    What leads you to believe this?

    I havent run a benchmark but my guess would be that fpassthru() is more efficient that a block based operation, as it is a native function.

    Another way would be to use readfile(). It probably will not make a big difference but saves fopen() and fclose() calls.
    PHP Code:
    <?php
        header
    ("Content-Disposition: attachment; filename={$this->settings['realname']}");
        
    header("Content-Type: {$this->settings['mimetype']}");
    //header("Content-type: application/octet-stream");
        
    header("Content-Length: {$this->settings['filesize']}");
    //header("Connection: close");
        
    header("Pragma: no-cache");
        
    readfile('/path/to/download');
        
    flush();
        exit();
    ?>
    Sitemeer.com - Is your site up?
    Multi-Location Service Availability Check ● yes, we do HTTPS & IDN!


  6. #6
    Join Date
    Nov 2003
    Posts
    682
    If your server has gzip compression on, you might be sending the wrong value for the content-length header.

  7. #7
    Join Date
    Aug 2002
    Location
    Canada!
    Posts
    159
    I have the following set in my php.ini

    output_buffering = On
    output_handler = ob_gzhandler
    zlib.output_compression = Off

  8. #8
    Join Date
    Nov 2003
    Posts
    682
    Ok, that's not the problem then.

  9. #9
    Which browser are you using for download and does your filename has spaces in the name?
    Dyslexics Have More Fnu

  10. #10
    Join Date
    Aug 2002
    Location
    Canada!
    Posts
    159
    No spaces, I've been trying it in Firefox but I'll give it a go in Internet Explorer later. I'm not sure what browser my users are using but they're the ones who pointed out the error.

    I'd like to remind everyone that it works for the 2MB file but not for bigger files. I tried to get the 4MB+ zip and the 7MB+ zip. Both gave me corrupted zip files.

  11. #11
    Join Date
    Nov 2003
    Posts
    682
    Are your settings for upload_max_filesize and post_max_size large enough for that?

  12. #12
    Join Date
    Aug 2002
    Location
    Canada!
    Posts
    159
    Both upload_max_filesize and post_max_size are 15MB. Is that big enough or should I be multiplying some numbers to get the correct one?

    And I didn't turn on safemode.

  13. #13
    Join Date
    Aug 2002
    Location
    Canada!
    Posts
    159
    Anybody have any more ideas?

    I'm willing to give the first person to figure out what's wrong $5USD if that proves to be any sort of motivation.

  14. #14
    Join Date
    Sep 2005
    Location
    India
    Posts
    750
    What leads you to believe this?

    I havent run a benchmark but my guess would be that fpassthru() is more efficient that a block based operation, as it is a native function.
    fpassthru() uses more memory than a fread() loop. You can verify this by using the memory_get_usage() function.
    Darsh Web Solutions : Web Design, PHP Development, E-Commerce Solutions

    PHP Tutorials : Tutorials and scripts for beginners

  15. #15
    Join Date
    Aug 2001
    Posts
    5,068
    Quote Originally Posted by Jatinder
    fpassthru() uses more memory than a fread() loop. You can verify this by using the memory_get_usage() function.
    Could you post your test script and/or benchmark results please?

    I am not familiar with exact technical details of fpassthru() or readfile() but I would not assume that it loads the entire file into the memory (which would be the only possible reason for a notably increased memory usage) but streams it to the client (where a bigger block size might actually cause a slightly higher memory consumption, but should be made up for by the faster code execution).

    Again, I havent run a benchmark and dont know exactly how readfile() is handled internally, but if you have these values/information please post them.
    Sitemeer.com - Is your site up?
    Multi-Location Service Availability Check ● yes, we do HTTPS & IDN!


Posting Permissions

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