php檔案下載

來源:互聯網
上載者:User

標籤:

 1 <?php 2        //對函數的說明 3        //參數說明 $file_name 檔案名稱 4        //               $file_sub_dir: 下載檔案的子路徑 ‘"/xxx/xxx/" 5        function down_file($file_name,$file_sub_dir){ 6               //死去活來,示範下載一個圖片. 7               //如果檔案是中文. 8                9               10               //原因 php檔案函數,比較古老,需要對中文轉碼 gb231211               $file_name=iconv("utf-8","gb2312",$file_name);12               //絕對路徑13               $file_path=$_SERVER[‘DOCUMENT_ROOT‘].$file_sub_dir.$file_name;14               //如果你希望絕對路徑15               16               //1.開啟檔案17               echo $file_path;18               if(!file_exists($file_path)){19                      echo "檔案不存在!";20                      return ;21               }22               $fp=fopen($file_path,"r");23               24               //擷取下載檔案的大小25               $file_size=filesize($file_path);26               // if($file_size>30){27                      28               //        echo "<script language=‘javascript‘>window.alert(‘過大‘)</script>";29               //        return ;30               // }31               //返回的檔案32               header("Content-type: application/octet-stream");33               //按照位元組大小返回34               header("Accept-Ranges: bytes");35               //返迴文件大小36               header("Accept-Length: $file_size");37               //這裡用戶端的彈出對話方塊,對應的檔案名稱38               header("Content-Disposition: attachment; filename=".$file_name);39               //向用戶端回送資料40               41               $buffer=1024;42               //為了下載的安全,我們最好做一個檔案位元組讀取計數器43               $file_count=0;44               //這句話用於判斷檔案是否結束45               while(!feof($fp) && ($file_size-$file_count>0) ){46                      $file_data=fread($fp,$buffer);47                      //統計讀了多少個位元組48                      $file_count+=$buffer;49                      //把部分資料回送給瀏覽器;50                      echo $file_data;51               }52               //關閉檔案53               fclose($fp);54        }55        //測試函數是否可用56        down_file("Day by Day 完整版.mp4","/html5/");57 ?>

 

php檔案下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.