Jump to content

File download is corrupted

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
jaguar

jaguar

    Newbie

  • Members
  • Pip
  • 2 posts
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:



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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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

#3
Guest_Jordan_*

Guest_Jordan_*
  • Guests
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:

          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:

@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:

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.

#4
jaguar

jaguar

    Newbie

  • Members
  • Pip
  • 2 posts
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.


$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?