VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Topic started by: innato on October 05, 2016, 20:24:00 PM

Title: SOLVED: ZIP file download fails
Post by: innato on October 05, 2016, 20:24:00 PM
This may not be a VM issue at all, but I am just wondering if someone else has the same problem and - above all - a solution!!

Using VM3.0.18 on J3.6.2 and PHP 5.6.26.
I have isTraxx virtual download plugin installed with my VM3 webshop where I offer PHP software in the form of ZIP file downloads (zipped with WinZip v19.5 Pro). If I complete an order and click the file download link, the file can be downloaded to disk and the file size is OK. But when I try to unzip the downloaded file with WinZip, there is an error saying that the file is an invalid archive (see attached picture).
If I download the same ZIP file from its server location through FTP (i.e. the same source as the VM download), the file size is the same and the file can be unzipped with the same WinZip app without problems.
Curiously, if the downloaded file is a PDF file, there is no problem at all. If the file is a JPG image, I have the same issue (error "cannot open file"). I have not tested other file formats.
Title: Re: ZIP file download fails
Post by: GJC Web Design on October 05, 2016, 23:55:42 PM
I don't know if this is the prob but we had a lot of problems with PhocaDownloads with similar symptoms

It was with jpgs .. we would get an invalid file error after downloading but we knew the images were perfect

I found the reason.. for some reason an extra blank line was added to the file by the Phoca thing .. u could see it if u opened the jpg in a txt editor
sadly never found a solution..  they now use a different file download extension

try opening the local copy and the downloaded in say Notepad++  and see if they are byte for byte the same
Title: Re: ZIP file download fails
Post by: innato on October 06, 2016, 10:25:55 AM
Wow GJC!! You are amazing. You are completely right, there was an extra character (ascii 10) added at the beginning of the file. When I removed it, the file was recognised again.
Strange enough I had compared both files (downloaded vs FTP-ed) using CSDiff file comparison prog, but it said that the files were identical. Also the files sizes were identical (probably made equal by adding an additional ascii 0).
I'll investigate further and also open a ticket with isTraxx.
Many thanks and again my compliments!!
Title: Re: ZIP file download fails
Post by: GJC Web Design on October 06, 2016, 13:39:22 PM
Thanks-- but sadly we never got to the bottom of the problem
It does seem to be server related as I have never seen it on any of my clients installs using Istraxx etc
the one above was a blow in client

I did get as far as this  answer -- suggesting white space in the php file serving the file

from Stack Overflow

###############

My guess would be that you're outputting white-space characters in your PHP code. Probably line feeds. This is a very common cause of file corruption when serving binary files via PHP, and can be hard to spot. It is also very typical of cases where some file types are corrupted and others aren't, because some file types can cope with the extra white space.

White space can creep into a PHP program very easily, simply by having line feeds in your source code outside of the <?php and ?> markers.

Check your PHP program - and all your includes - to ensure they don't have any trailing blank lines after the closing ?> at the end of the program. Also check at the top of the files before the <?php marker, but blank lines at the end of the program is much more common.

###############

so what is required is to go thru the files serving the download file and eliminate all whitespace if the above is correct
Title: Re: ZIP file download fails
Post by: innato on October 06, 2016, 16:45:02 PM
Quote from: GJC Web Design on October 06, 2016, 13:39:22 PM
so what is required is to go thru the files serving the download file and eliminate all whitespace if the above is correct
That is exactly what I intend to do. If I find something useful, I will post it here.
Thanks again and regards.
Title: Re: ZIP file download fails
Post by: innato on October 06, 2016, 21:23:36 PM
= PROBLEM SOLVED =
The isTraxx virtual download plug-in includes the file /plugins/vmcustom/istraxx_download/istraxx_download.php

This file has the following three lines of code on lines 1078-1080:

1078 $connection = connection_status ();
1079
1080 while (!feof ($fp) and (connection_status () == 0)) {


If you insert two lines between line 1078 and 1080 as follows

1078 $connection = connection_status ();
1079
1080 ob_end_clean();
1081 ob_start();
1082
1083 while (!feof ($fp) and (connection_status () == 0)) {


then the problem is gone!
Apparently it's a matter of clearing the output buffer before starting the file download.
Title: Re: SOLVED: ZIP file download fails
Post by: GJC Web Design on October 06, 2016, 23:13:12 PM
Gave Max a heads up..  thanks
Title: Re: SOLVED: ZIP file download fails
Post by: Milbo on October 06, 2016, 23:41:31 PM
Interesting. The problem is that somewhere your page does echo something. We tested the plugin with zips, actually you used the plugin to download the plugin :-)

But it is an interesting idea to add it to prevent this problem, But I wonder, this should be enough ob_end_clean();
Title: Re: SOLVED: ZIP file download fails
Post by: innato on October 07, 2016, 10:26:27 AM
My source is here http://php.net/manual/en/function.fread.php
See under User Contribution Note #4 by randym. He wrote:
QuoteConcerning [problems with UTF-8 and] downloading Zip files I found that simply adding 3 lines of code before starting the fread to the buffer for delivery in all browsers solved the problem.
In fact he adds three lines namely
Quoteob_end_clean();
    ob_start();
    header( 'Content-Type:' );

I was not sure about adding the last line because a content type header had already been defined a few lines earlier (in the plug-in I mean).

EDIT: The problem does not exist with JPG and not with PDF, meaning no extra chars are added (or removed).
Title: Re: SOLVED: ZIP file download fails
Post by: Milbo on October 10, 2016, 13:22:06 PM
Interesting, which php version do you use? Reallly php5.6? I wonder, maybe it is a php bug.
Title: Re: SOLVED: ZIP file download fails
Post by: innato on October 10, 2016, 22:44:25 PM
Quote from: Milbo on October 10, 2016, 13:22:06 PM
Interesting, which php version do you use? Reallly php5.6? I wonder, maybe it is a php bug.
I use PHP 5.6.26 for sure
Title: Re: SOLVED: ZIP file download fails
Post by: innato on March 06, 2017, 12:23:40 PM
Quote from: Milbo on October 10, 2016, 13:22:06 PM
VM3.0.18 on J3.6.5 and PHP 5.6.
With the latest isTraxx virtual download plugin version 2.4.14 the problem is still the same AND the solution is still the same (adding 2 lines of code, see my earlier post in this thread).