Zip File Corruption and Smarty

This is a discussion on Zip File Corruption and Smarty within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi, Having a weird problem and hope somebody can help. Running PHP 4.3.9 on FreeBSD 5.3 (hosting ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-09-2004
Neophyte
 
Posts: n/a
Default Zip File Corruption and Smarty

Hi,

Having a weird problem and hope somebody can help.

Running PHP 4.3.9 on FreeBSD 5.3 (hosting service).

Have a script which allows users to download zip files. After some
teething problems settled on the following code which worked.

ini_set('zlib.output_compression', 'Off');
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-type: application/zip');
header ("Content-Length: " . filesize($path . $filename));
header('Content-Disposition: attachment; filename="' . $filename .
'"');
readfile($path . $filename);

Recently I used Smarty to replace inline HTML with templates.
Suddenly the zip downloads are all corrupted. If I go back to the
previous version of the script - without Smarty - the zip downloads
work fine again.

Any suggestions welcome.

Reply With Quote
  #2 (permalink)  
Old 12-09-2004
Andy Hassall
 
Posts: n/a
Default Re: Zip File Corruption and Smarty

On Thu, 09 Dec 2004 22:45:43 GMT, Neophyte <neophyte@home.org> wrote:

>Having a weird problem and hope somebody can help.
>
>Running PHP 4.3.9 on FreeBSD 5.3 (hosting service).
>
>Have a script which allows users to download zip files. After some
>teething problems settled on the following code which worked.
>
>ini_set('zlib.output_compression', 'Off');
>header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
>header('Content-type: application/zip');
>header ("Content-Length: " . filesize($path . $filename));


I wonder slightly about this; from an extremely brief reading of the HTTP
spec, Content-length indicates the length of the entity body. Does this take
into account transfer encoding methods? Wouldn't that cause the length of the
data to differ from the length of the encoded data sent in the response?

>header('Content-Disposition: attachment; filename="' . $filename .
>'"');
>readfile($path . $filename);
>
>Recently I used Smarty to replace inline HTML with templates.
>Suddenly the zip downloads are all corrupted. If I go back to the
>previous version of the script - without Smarty - the zip downloads
>work fine again.


I can't work out where Smarty comes into the mix here - you're not using
Smarty to actually serve the zipfiles, are you?

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Reply With Quote
  #3 (permalink)  
Old 12-09-2004
Neophyte
 
Posts: n/a
Default Re: Zip File Corruption and Smarty

Hi Andy,

Andy Hassall <andy@andyh.co.uk> wrote:

[snip]
>
> I wonder slightly about this; from an extremely brief reading of the HTTP
>spec, Content-length indicates the length of the entity body. Does this take
>into account transfer encoding methods? Wouldn't that cause the length of the
>data to differ from the length of the encoded data sent in the response?
>

The files are always downloading with the correct size (I even
downloaded some with ftp for comparison). It's just the content that
is somehow corrupted so that they aren't valid zips when they arrive.

>>header('Content-Disposition: attachment; filename="' . $filename .
>>'"');
>>readfile($path . $filename);
>>
>>Recently I used Smarty to replace inline HTML with templates.
>>Suddenly the zip downloads are all corrupted. If I go back to the
>>previous version of the script - without Smarty - the zip downloads
>>work fine again.

>
> I can't work out where Smarty comes into the mix here - you're not using
>Smarty to actually serve the zipfiles, are you?


It doesn't make any sense to me either! But the introduction of
Smarty is the only change I've made and the file corruption started
soon after. If I go back to the old non-Smarty version the downloads
stop getting corrupted.

I'm baffled, but there it is.

Reply With Quote
  #4 (permalink)  
Old 12-09-2004
Andy Hassall
 
Posts: n/a
Default Re: Zip File Corruption and Smarty

On Thu, 09 Dec 2004 23:21:01 GMT, Neophyte <neophyte@home.org> wrote:

>> I can't work out where Smarty comes into the mix here - you're not using
>>Smarty to actually serve the zipfiles, are you?

>
>It doesn't make any sense to me either! But the introduction of
>Smarty is the only change I've made and the file corruption started
>soon after. If I go back to the old non-Smarty version the downloads
>stop getting corrupted.
>
>I'm baffled, but there it is.


Have you put the corrupted files into a text editor to see if there's anything
obvious like PHP error messages embedded in the file?

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Reply With Quote
  #5 (permalink)  
Old 12-09-2004
Neophyte
 
Posts: n/a
Default Re: Zip File Corruption and Smarty

[snip]
>>
>> I can't work out where Smarty comes into the mix here - you're not using
>>Smarty to actually serve the zipfiles, are you?

>
>It doesn't make any sense to me either! But the introduction of
>Smarty is the only change I've made and the file corruption started
>soon after. If I go back to the old non-Smarty version the downloads
>stop getting corrupted.
>
>I'm baffled, but there it is.


Just to clarify. No, I'm not using Smarty to serve zipfiles. If
there's some problem (eg. file doesn't exist) I display an error page.
Originally this was HTML inline in the script, then I converted it to
Smarty. If everything is OK I just send the download headers direct
from PHP and then the file.

Reply With Quote
  #6 (permalink)  
Old 12-09-2004
Neophyte
 
Posts: n/a
Default Re: Zip File Corruption and Smarty

Andy Hassall <andy@andyh.co.uk> wrote:

>On Thu, 09 Dec 2004 23:21:01 GMT, Neophyte <neophyte@home.org> wrote:
>
>>> I can't work out where Smarty comes into the mix here - you're not using
>>>Smarty to actually serve the zipfiles, are you?

>>
>>It doesn't make any sense to me either! But the introduction of
>>Smarty is the only change I've made and the file corruption started
>>soon after. If I go back to the old non-Smarty version the downloads
>>stop getting corrupted.
>>
>>I'm baffled, but there it is.

>
> Have you put the corrupted files into a text editor to see if there's anything
>obvious like PHP error messages embedded in the file?


I've had a look at them in a hex viewer. The difference from a known
good version of the file and the corrupt one is that the corrupt one
has "200A" hex prepended to it and the last two bytes of the file have
been truncated; leaving it the right size, but bad.

Reply With Quote
  #7 (permalink)  
Old 12-09-2004
Andy Hassall
 
Posts: n/a
Default Re: Zip File Corruption and Smarty

On Thu, 09 Dec 2004 23:51:55 GMT, Neophyte <neophyte@home.org> wrote:

>Andy Hassall <andy@andyh.co.uk> wrote:
>
>>On Thu, 09 Dec 2004 23:21:01 GMT, Neophyte <neophyte@home.org> wrote:
>>
>>>> I can't work out where Smarty comes into the mix here - you're not using
>>>>Smarty to actually serve the zipfiles, are you?
>>>
>>>It doesn't make any sense to me either! But the introduction of
>>>Smarty is the only change I've made and the file corruption started
>>>soon after. If I go back to the old non-Smarty version the downloads
>>>stop getting corrupted.
>>>
>>>I'm baffled, but there it is.

>>
>> Have you put the corrupted files into a text editor to see if there's anything
>>obvious like PHP error messages embedded in the file?

>
>I've had a look at them in a hex viewer. The difference from a known
>good version of the file and the corrupt one is that the corrupt one
>has "200A" hex prepended to it and the last two bytes of the file have
>been truncated; leaving it the right size, but bad.


The file wouldn't happen to be 8202 bytes long, would it?

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Reply With Quote
  #8 (permalink)  
Old 12-10-2004
Hilarion
 
Posts: n/a
Default Re: Zip File Corruption and Smarty

> introduction of Smarty is the only change I've made and the file corruption
> started soon after. If I go back to the old non-Smarty version the downloads
> stop getting corrupted.


Maybe by the way you added some .htaccess files or something which
affects HTTP headers.

Hilarion


Reply With Quote
  #9 (permalink)  
Old 12-12-2004
Neophyte
 
Posts: n/a
Default Re: Zip File Corruption and Smarty - Solved

First, thanks to everyone who responded. Much appreciated.

I finally solved the problem.

I'd inherited from the base Smarty class so I could specify
application specific Smarty settings in one place.

The source file for the class definition somehow got an extra
<space><linefeed> after the closing "?>". When I included this in the
main program PHP output the trailing characters to the browser as
literal text.

For regular HTML pages this is not a problem. But for the file
download it was disastrous. It caused the client to include "200A" at
the start of the downloaded file and, because it was expecting
filesize() bytes, it truncated the last two bytes of the file as well.

Everything works fine now.

Thanks again.

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:36 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0