php(http協議)檔案下載的實現代碼

來源:互聯網
上載者:User
一個使用http協議實現檔案下載的php代碼,對於深入理解php檔案下載的原理,是個不錯的例子,有需要的朋友,真的應該參考學習下。

1、檔案下載代碼

0){  $file_data=fread($fp,$buffer);  //統計讀取多少個位元組數  $file_count+=$buffer;  //把部分資料返回給瀏覽器  echo $file_data; } //關閉檔案 fclose($fp);?>

2、封裝函數:

50){   echo "檔案太大不能下載";   return ;  }*/  //http 下載需要的回應標頭   header("Content-type: application/octet-stream"); //返回的檔案   header("Accept-Ranges: bytes");   //按照位元組大小返回  header("Accept-Length: $file_size"); //返迴文件大小  header("Content-Disposition: attachment; filename=".$file_name);//這裡用戶端的彈出對話方塊,對應的檔案名稱  //向用戶端返回資料  //設定大小輸出  $buffer=1024;  //為了下載安全,我們最好做一個檔案位元組讀取計數器  $file_count=0;  //判斷檔案指標是否到了檔案結束的位置(讀取檔案是否結束)  while(!feof($fp) && ($file_size-$file_count)>0){   $file_data=fread($fp,$buffer);   //統計讀取多少個位元組數   $file_count+=$buffer;   //把部分資料返回給瀏覽器   echo $file_data;  }  //關閉檔案  fclose($fp); }  file_dowm("bjnihao.jpg","/http/dowm/");?>
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.