We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36805
    • 354 Posts
    I have made a simple paypal based shop system for buying files. The procedure at some point (after payment handling) envolves the client getting an email with a link to a php download script with an url embedded download code. This code is valid for several weeks in which the file can be downloaded.

    The problem is with the download script itself. Currently it works if I set it up for downloading an mp3 but as soon as I try it for a zip file most machines that tested this got a corrupted unopenable file. The testers experiencing this reported a correct filesize however. Strangely at our office it worked on several computers.

    I have tried setting the MIME type (which is currently application/octet-stream) to application/zip and later to application/x-zip-compressed but it does not change anything.

    Anyone has some ideas about what could be wrong with my code or maybe server setup?

    	/* sql quiries to get $product_id and $expired as well as download code verification etc. omitted for this post */
    	
    	if ($expired != 1)
    	{		
    		$url = "/serverpath/to/mydomain.tld/shop-files/".$filename;
    		
    		$mm_type="application/octet-stream";
    		
    		header("Pragma: public");
    		header("Expires: 0");
    		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    		header("Cache-Control: public", false);
    		header("Content-Description: File Transfer");
    		header("Content-Type: " . $mm_type);
    		header("Accept-Ranges: bytes");
    		header('Content-Disposition: attachment; filename="'.basename($url).'"');
    		header("Content-Transfer-Encoding: binary");
    		header("Content-Length: " .filesize($url) );
       
    		if ($handle = fopen ($url, 'rb'))
    		{
    			ob_start();
    			
    		  while(!feof($handle) && connection_status() == 0)
    		  { 
    		    print(fread($handle, 1024*8));
    		    set_time_limit(0);
    		    ob_flush();
    		    flush();
    		  }
    		  
    		  ob_end_flush();
    			fclose ($handle);
    			
    		} else echo "File could not be found";
    		
    	} else echo "Your downloadlink is expired.";
    
      • 36805
      • 354 Posts
      Quote from: ganeshXL at Jul 17, 2007, 03:25 AM

      Check if you have some whitespace anywhere before the header.
      I am unsure about what you mean here, note that the source paste is pretty much complete, there are no header() calls before or after it.
      Quote from: ganeshXL at Jul 17, 2007, 03:25 AM

      Try using header(’Content-Type: application/force-download’);
      This did not change anything unfortunately.
      Quote from: ganeshXL at Jul 17, 2007, 03:25 AM

      and finally, also check if your FTP-uploads are corrupted - e.g. ascii instead of binary mode.
      I have been working on websites on this server for quite some time and did not experience any file corruption until now.
        • 33372
        • 1,611 Posts
        Have you tried not setting any MIME type at all and letting the browser manage that? In general I don’t think I bother setting a MIME type (or any other header info) for zip files and I’ve never had a problem that way. This script is not being run from within MODx, right (since MODx sends its own headers, of course)?
          "Things are not what they appear to be; nor are they otherwise." - Buddha

          "Well, gee, Buddha - that wasn't very helpful..." - ZAP

          Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
          • 22303 MODX Staff
          • 10,725 Posts
          Make sure you’re not using zlib compression, or if you are, make allowances for it so that it is not enabled when the download script is sending your files. This was a problem for quite some time with the MODx downloads themselves due to this, and it only seems to affect certain clients.
            • 36805
            • 354 Posts
            Quote from: OpenGeek at Jul 17, 2007, 01:07 PM

            Make sure you’re not using zlib compression, or if you are, make allowances for it so that it is not enabled when the download script is sending your files. This was a problem for quite some time with the MODx downloads themselves due to this, and it only seems to affect certain clients.
            Thanks everyone for your replies.

            How do i disable the zlib compression just for the script itself?
              • 22303 MODX Staff
              • 10,725 Posts
              Quote from: MrDutchy at Jul 17, 2007, 04:18 PM

              How do i disable the zlib compression just for the script itself?

              Well, one way is to add an .htaccess in the directory containing the php file from which the file is being downloaded with the following content:
              php_flag zlib.output_compression Off
              
                • 33372
                • 1,611 Posts
                That totally makes sense. You could use an .htaccess file that turns this off in your script’s subdirectory or using a files limit. Look here for more info on how to turn it off in PHP for just that script: http://php.net/manual/en/ref.zlib.php
                  "Things are not what they appear to be; nor are they otherwise." - Buddha

                  "Well, gee, Buddha - that wasn't very helpful..." - ZAP

                  Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
                  • 36805
                  • 354 Posts
                  The zlib was indeed the cause of the file corruption. Many thanks! smiley