We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Hello smart and friendly MODx folks... I could post this in some other site, but I thought I’d try my favorite peeps first...

    I built a page that checks whether a member is logged in with the correct permissions (no problem), then it streams a zip file from outside the document root using a script something like this:

    http://www.domain.com/downloader.php?file=somefile.zip

    This ensures that only logged-in members can download the assets (for legal reasons). In development, this worked fine, but now all of a sudden, we’re experiencing strange problems:

    The guts of this essentially center around the following PHP lines:
    			$fullfilename = '/full/path/to/file.zip';
    			$f = 'filename_displayed_to_user.zip';
    			  header("Content-type: application/zip");
    			  header("Content-Disposition: attachment; filename=$f");
    			  readfile("$fullfilename");


    The zip files download as expected -- that is the browser automatically downloads the file when the above code is run, but we can’t double click the zip files to open them. We know that the zip files are healthy and uncorrupted because when we put them somewhere in the document root (e.g. http://www.domain.com/folder/somefile.zip), we can download the files without any problems. But when we access the files via the above URL (i.e. streaming the files via the above PHP code), the downloaded zip files do not immediately open. On a Mac, double-clicking the zip file causes it to create another file with a .cpgz extension. huh What is this? Stuffit can’t open it... but Springy can (a 3rd party app). Nothing on the Windows side seems to be able read inside the files...

    Springy allows us to extract everything "normally"... but what on earth is causing this? Some of the files are large (e.g. 80MB), but this problem is also occurring with our small test files (e.g. 10kb).

    If anyone has any ideas, I’d love to hear them. I haven’t changed anything fundamentally in the code since this was tested successfully in development.
      • 26903
      • 1,336 Posts
      Try adding these header lines too your script :-

      header("Content-Transfer-Encoding: binary");
      header("Content-Length: " . filesize($fullfilename))

      This is usually an encoding problem of some sort, as in this link http://www.codingforums.com/archive/index.php/t-157392.html
        Use MODx, or the cat gets it!
      • No dice.... implementing those changes had no effect. Any other ideas before I bludgeon my way through every possible cause of this?
        • Make sure gzip compression is not enabled in your .htaccess. This will add gzip compression to the zip file and override the content-type...
          • Mmm... I wouldn’t have thought of that. But these 2 lines are commented out:
            #php_flag zlib.output_compression On
            #php_value zlib.output_compression_level 5


            It is running PHP 4 apparently:
            AddHandler php4-script .php


            It’s hosted on Media Temple grid servers, which sometimes do weird things with PHP 4 and 5 (I’m not sure how they pull it off to be running both versions simultaneously).

            Any other ideas?
              • 37523
              • 141 Posts
              I know this is an old thread, I am still going to try and revive it as I am experiencing the same issue as mentioned at the start of this.
              I am very stumped and frustrated. My code is as follows:

                      header("Content-type: application/zip");
                      header("Content-Disposition: attachment; filename=".$zip_name);
                      header("Content-Transfer-Encoding:binary");
                      header("Content-Length:".$zipFileSize);
                      readfile($zip_name);
                      unlink($zip_name);
              


              When I look at the generated file on the server and unzip all looks good. However running the script and double clicking on the downloaded file on my mac adds a .cpgz extension.

              I looked at the .htaccess file on the server and attempted to change (the mentioned above) it to:
              #php_flag zlib.output_compression Off
              #php_value zlib.output_compression_level 5


              No luck with that one.

              Are there some other ideas I could try?

              Everett, did you find a solution?

              Many thanks in advance all.
                Glyn Szasz
                Sydney, Australia

                Happy to help (when can) and happy to learn

                If you are a Sydney based MODX developer would love to hear from you. Please contact me.
                • 37523
                • 141 Posts
                Well I have found a solution to this problem. VERY VERY Happy. Been racking my brain and trawling the net for about 2 days on this one.

                The problem seemed to be that the page with the form on it had the php to build the zip and force the browser to download. The fix was/is to set the form action to be on another page or directly to a php script, say in the root.

                I think that the header part of the script was not being processed correctly as headers had already been sent and extra unwanted headers were being sent as part of the already created zip thus the browser is pushing out a zip + extra unwanted parts equaling a corrupted zip file.

                The solution and lesson learned in this one is always to process a form using an external file to the form.

                I hope this solution helps anyone looking at this really old thread.
                  Glyn Szasz
                  Sydney, Australia

                  Happy to help (when can) and happy to learn

                  If you are a Sydney based MODX developer would love to hear from you. Please contact me.