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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
[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. |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
> 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 |
|
|||
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|