首頁  >  文章  >  後端開發  >  php下載遠端大文件的實例(取得遠端檔案大小)

php下載遠端大文件的實例(取得遠端檔案大小)

微波
微波原創
2017-06-28 11:50:321546瀏覽

下面小編就為大家帶來一篇php下載遠端大檔案(取得遠端檔案大小)的實例。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟著小編過來看看吧

廢話不多說,直接上程式碼

#
<?php
  // 暂不支持断点续传
  // $url = &#39;http://www.mytest.com/debian.iso&#39;; 不知道为何获取本地文件大小为0
  $url = &#39;http://192.168.8.93/download/vm-672/18/0.vmdk&#39;;
  $file = basename($url);
  $header = get_headers($url, 1);
  $size = $header[&#39;Content-Length&#39;];

  $fp = fopen($url, &#39;rb&#39;);
  if ($fp === false) exit(&#39;文件不存在或打开失败&#39;);

  header(&#39;Content-Description: File Transfer&#39;);
  header(&#39;Content-Type: application/octet-stream&#39;);
  header(&#39;Content-Disposition: attachment; filename="&#39;.$file.&#39;"&#39;);
  header(&#39;Content-Transfer-Encoding: binary&#39;);
  header(&#39;Expires: 0&#39;);
  header(&#39;Cache-Control: must-revalidate, post-check=0, pre-check=0&#39;);
  header(&#39;Pragma: public&#39;);
  header(&#39;Content-Length: &#39; . $size);

  ob_clean();
  ob_end_flush();
  set_time_limit(0);
  
  $chunkSize = 1024 * 1024;
  while (!feof($fp)) {
    $buffer = fread($fp, $chunkSize);
    echo $buffer;
    ob_flush();
    flush();
  }
  fclose($fp);
  exit;


以上是php下載遠端大文件的實例(取得遠端檔案大小)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn