Jump to content

File download is corrupted


jaguar080

Recommended 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

Link to comment
Share on other sites

How is it being corrupt?

 

The way I found out if something was an issue with a download I would open up the corrupt file via notepad or something. What I found when my downloads were corrupt was that the header of the file was including an error message. In my case it was because the actual filesize was different than the filesize I was passing to the header function when I would initiate the code to prompt the user to save the file to a location.

Link to comment
Share on other sites

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.

 

this is the bit where i set the file size in the headers, how can this go wrong?

 


header('Content-Length: ' . filesize($filenameandpath) ); 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.