problem forcing download on zip files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
hanji
Forum Commoner
Posts: 46
Joined: Fri Apr 29, 2005 3:23 pm

problem forcing download on zip files

Post by hanji »

I'm encountering a strange problem with fopen/fpassthru && headers for downloading zip files. My dowload appears to be tru
ncated by 3 bytes. This happens with IE/Firefox and with firefox on Linux.

I have an application that stores files on the server (named as a hash). When user downloads a file (provided by database,
I rename the hash back to it's normal name, and read it to the browser and force-download.

This works great for images, word docs, etc, but zip files will produce the following error:

Code: Select all

---------------------------
WinZip
---------------------------
Cannot open file: it does not appear to be a valid archive.
If you downloaded this file, try downloading the file again.
and in linux -> unzip:

Code: Select all

unzip testCounty.ZIP 
Archive:  testCounty.ZIP
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of testCounty.ZIP or
        montCounty.ZIP.zip, and cannot find testCounty.ZIP.ZIP, period.

I'm able to open the 'hashed' named file without a problem. The problem occurs after file has been renamed and downloaded.

Here is file listing showing the different file sizes:

Code: Select all

-rw-rw-r--   1 hanji users      78276 Apr 29 11:42 4adddf640a2ca7568ebd1549d1978fa1
-rw-rw-r--   1 hanji users      78273 Apr 29 11:42 testCounty.ZIP
These two are the same file. Notice the 'normal' named file is 3 bytes less than the hash'd named file.

When I use hexedit and cat a file with hexdumps of both files.. and diff them.. this is what I get:

Code: Select all

4893,4894c4893,4894
< 00131c0 0001 0000                              
< 00131c4
---
> 00131c0 0001                                   
> 00131c1
< is hashed file
> is normal named file

So I'm missing 0000 in the final downloaded file.

Here is my code..

Code: Select all

// $FileVal contains full path to 'hashed' file name
// $FileName is the 'normal' filename value
// make sure the file exists before sending headers
if(!$fdl=@fopen($FileVal,'rb')){
   die(&quote;Cannot Open File!&quote;);
} else {
  header(&quote;Cache-Control: &quote;);// leave blank to avoid IE errors
  header(&quote;Pragma: &quote;);// leave blank to avoid IE errors
  header(&quote;Content-type: application/octet-stream&quote;);
  header(&quote;Content-Disposition: attachment; filename=\&quote;&quote;.$FileName.&quote;\&quote;&quote;);
  header(&quote;Content-length:&quote;.(string)(filesize($FileVal)));
  @fpassthru($fdl);
}
I've tried multipe methods.. including (fread, file_get_contents, and fpassthru). All result with the same truncated file length and unusable zip file.

Any help is greatly appreciated!
thanks
hanji
User avatar
hanji
Forum Commoner
Posts: 46
Joined: Fri Apr 29, 2005 3:23 pm

Post by hanji »

I forgot to post my environment...

Server version: Apache/2.0.52 (w/mod_security)
PHP 4.3.11 (safe_mode enabled)
Linux 2.6.11-gentoo-r6

File corruption happens on Linux/Gentoo/fluxbox and WindowsXP. Files are originally stored on linux filesystem (reiserfs)

hanji
User avatar
hanji
Forum Commoner
Posts: 46
Joined: Fri Apr 29, 2005 3:23 pm

Post by hanji »

I'm noticing the same behaviour with MSWord docs. They still open and are not 'corrupted', but the filesize is less than the original.

Any ideas? I'll be heading into desperate mode soon.

Thanks much!
hanji
Post Reply