Closed Thread
Results 1 to 4 of 4

Thread: File download is corrupted

  1. #1
    jaguar is offline Newbie
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Exclamation File download is corrupted

    Hey all,

    I have a site that sells mp3's. The mp3 files are grouped; so the download is a zip file of about 70-100 MB. The zip files are in .htaccess protected directory of my webserver. The code i use to send the file to the client browser is the following:

    Code:

    function vmReadFileChunked($filename,$retbytes=true) {
        
    $chunksize 1*(1024*1024); // how many bytes per chunk
        
    $buffer '';
        
    $cnt =0;
        
    // $handle = fopen($filename, 'rb');
        
    $handle fopen($filename'rb');
        if (
    $handle === false) {
            return 
    false;
        }
        while (!
    feof($handle)) {
            
    $buffer fread($handle$chunksize);
            echo 
    $buffer;
            
    sleep(1);
            
    ob_flush();
            
    flush();
            if (
    $retbytes) {
                
    $cnt += strlen($buffer);
            }
        }
        
    $status fclose($handle);
        if (
    $retbytes && $status) {
            return 
    $cnt// return num. bytes delivered like readfile() does.
        
    }
        return 
    $status;

    Now, everything works fine with all my zip files, except one. There is one zip file that always gets corrupted when you download it. I have recreated the zip file many times, with many different compressors. I have made sure that the file is fine when you upload it through ftp. Yet the file continues to become corrupt on download. Oddly enough, if place outside of the .htaccess protected directory, the download is fine. This does not make sense as all the other zip files get downloaded just fine.

    so, the possible sources of the problem are:
    * the file is in an .htacces protected directory -- yet all other files download fine
    * the zip file is corrupted on ftp upload -- yet, if placed in another location within the server, the download is fine
    * the zip file is created with a lousy compressor -- windows, mac and linux compressors have been tried
    * the code to send the file is buggy -- yet all other files download fine

    Any insight on the true source of the problem and how it might be solved is greatly appreciated.

    Cheers

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: File download is corrupted

    Which browser are you using when the zip file is corrupted? Also, have you verified that it is not corrupt on the server?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Jordan Guest

    Re: File download is corrupted

    How large is the corrupted file compared to the others? Are you actually sending the correct size? What do you headers look like on the download script? They should look similar to this:

    Code:
              header("Pragma:  no-cache");
              
    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: $mtype");
              
    header("Content-Transfer-Encoding: binary"); 
              
    header("Content-Disposition: attachment; filename=\"".basename($file)."\"");
                
                
              if (
    ini_get('allow_url_fopen')) {
            if (
    extension_loaded('curl')) {
                    
    header("Content-Length: " remotefsize($file));  
            }
         }
          
                
    header("Accept-Ranges: bytes"); 
    Next, why did you write your own function to "chunk" the data? You can use something as simple as:

    Code:
    @readfile("$filename"); 
    If all you ever intend on sending the user is zip/compressed files you can simply forward their browser to the file URL. IE:

    Code:
    header("Location: " .$file); 
    There is no reason to force a download for these type of files as the browser will do that anyway.

    Here is some things you can do:
    1) Download the file. Once complete open it with a text-editor and see if any text data is getting added to the beginning or ending of the file that shouldn't be there.

    2) Does the file download when you link directly? If not, then it isn't your PHP script. You've corrupted the file somehow.

    3) Try to use the php readfile function above and see if it is just your chunk function.

    4) Make sure your headers are correct.

    I hope this helps. You can download the community project "ionFiles" here on CodeCall. If you look at the download.php file you can see how our PHP file download script works.

  5. #4
    jaguar is offline Newbie
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Re: File download is corrupted

    WingedPanther: Yes, I have tried downloading with with Firefox for ubuntu, windows and mac. Same thing happens with IE7 and safari. And yes i have verified that the file is not corrupt on the server because if i move it outside the protected directory, the download is fine.

    Jordan: thanks for the advice. because i'm selling downloads, i dont want anyone having a direct URL to them. that is why i'm using this script (which is part of virtuemart, btw) and a protected directory. all the files are of similar size.

    I just checked if there is an error message appended to the downloaded file, there is none. the file sizes are slightly different though, the downloaded file is smaller.

    Here is the code where i construct my headers (again, from virtuemart). it is very similar to the example you posted.

    Code:
    $datei DOWNLOADROOT $file_name;
    // dump anything in the buffer
    @ob_end_clean();

    header('Content-Type: ' $mime_type);
    header('Expires: ' gmdate('D, d M Y H:i:s') . ' GMT');
    header('Content-Length: ' filesize($datei) );

    if (
    $UserBrowser == 'IE') {
        
    header('Content-Disposition: attachment; filename="' $file_name '"');
        
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        
    header('Pragma: public');
    } else {
        
    header('Content-Disposition: attachment; filename="' $file_name '"');
        
    header('Pragma: no-cache');
    }
    /*** Now send the file!! ***/
    vmReadFileChunked$datei ); 
    i guess that the strangest thing is that all the code works for every other zip file i have. why should this particular one become corrupt?

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. HTTP Getting Browser To Open File And Not Download File
    By RhetoricalRuvim in forum General Programming
    Replies: 6
    Last Post: 08-05-2011, 12:14 AM
  2. Download file blocker
    By oneplace2u in forum PHP Development
    Replies: 3
    Last Post: 10-16-2010, 10:55 PM
  3. PHP Download File Stream
    By chinz85 in forum PHP Development
    Replies: 1
    Last Post: 06-04-2010, 02:07 AM
  4. Download a file
    By Lop in forum PHP Development
    Replies: 14
    Last Post: 05-16-2008, 10:07 AM
  5. Easy way to download a file
    By Saint in forum Managed C++
    Replies: 5
    Last Post: 09-11-2006, 12:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

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