简体   繁体   中英

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

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);
}

What could be wrong? Thanks!

Problem solved: Thank you all for the tips. It turns out that the downloaded packages contains a few extra white spaces which shouldn't be there. Silly.

I would suggest changing this:

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

to this

readfile($path);

You can find more info on readfile here , but basically it will do everything your loop does.

您的Content-Transfer-Encoding标头有一个额外的换行符 - 也许这会在最后一个标题和正文之间产生三个换行符?

You'd be better off just using readfile() . Unless you have ignore_user_abort on, then the script will terminate automatically if the user cancels anyway.

If you're still having problems, load the file up in a hex editor and compare the first few characters and the last few characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM