簡體   English   中英

在PHP中提供數字產品下載的最佳方式是什么?

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

客戶為下載鏈接付費的數字商業產品。

我已將所有壓縮文件(產品)放在web文檔根目錄之外,買家通過與paypal IPN集成的php腳本下載它們,以確保下載者是付費買家。

類似於: http//www.mysite.com/download.php?buyer_id = xxxx

到目前為止,這一切只能解決一個問題。 我通過上面的URL下載的zip文件已損壞,無法使用WinRAR正確打開。 但是,直接下載的zip很好。

我的代碼:

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

可能有什么不對? 謝謝!

問題解決了:謝謝大家的提示。 事實證明,下載的軟件包包含一些不應該存在的額外空白區域。 愚蠢。

我建議改變這個:

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

對此

readfile($path);

你可以在這里找到關於readfile的更多信息,但基本上它會完成你的循環所做的一切。

您的Content-Transfer-Encoding標頭有一個額外的換行符 - 也許這會在最后一個標題和正文之間產生三個換行符?

你最好只使用readfile() 除非你有ignore_user_abort,否則如果用戶取消,腳本將自動終止。

如果您仍然遇到問題,請在十六進制編輯器中加載文件,並比較前幾個字符和最后幾個字符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM