What’s the best way to supply download of a digital product in PHP?

Discussion in 'PHP' started by yangyang, Jan 27, 2009.

  1. #1
    Digital commercial products that customers pay for download link.

    I have put all the zipped files (products) outside of web document root and buyers download them via a php script which is integrated with paypal IPN to make sure the downloader is a paying buyer.

    Sort of like: http://www.mysite.com/download.php?buyer_id=xxx

    Thus far it all works with only one problem. The zip files I download via the URL above is corrupted and can't be opened correctly with WinRAR. However, directly downloaded zip is fine.

    My code:

                    $path = WAREHOUSE_PATH.'/products/'.$identifier.'.zip';
                            $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");
                            header("Content-Description: File Transfer");
                            header("Content-Type: " . $mm_type);
                            header("Content-Length: " .(string)(filesize($path)) );
                            header('Content-Disposition: attachment; filename="'.basename($path).'"');
                            header("Content-Transfer-Encoding: binary\n");
    
                            $fp = @fopen($path,"rb");
                            if ($fp) {
                              while(!feof($fp)) {
                                print(fread($fp, 1024*8));
                                flush();
                                if (connection_status()!=0) {
                                  @fclose($fp);
                                  die();
                                }
                              }
                              @fclose($fp);
                            }
    PHP:
    What could be wrong? Thanks!
     
    yangyang, Jan 27, 2009 IP
  2. mallorcahp

    mallorcahp Peon

    Messages:
    141
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This looks odd to me ...

    header('Content-Disposition: attachment; filename="'.basename($path).'"');

    I have a similar download which has this ...

    header('Content-Disposition: attachment; filename=' . basename($path));

    And instead of:

    $fp = @fopen($path,"rb"); if ($fp) { while(!feof($fp)) { print(fread($fp, 1024*8)); flush(); if (connection_status()!=0) { @fclose($fp); die(); } } @fclose($fp); }

    Try

    @readfile($path);
     
    mallorcahp, Jan 27, 2009 IP
  3. yangyang

    yangyang Banned

    Messages:
    757
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks! problem solved. Seems I had a few unwanted white spaces before the download. Silly.
     
    yangyang, Jan 31, 2009 IP