PHP學習筆記三十八【下載】

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   sp   檔案   資料   div   

<?php   //示範下載一個圖片   $file_name="SunSet.jpg";      $file_name=iconv("utf-8","gb2312",$file_name); //對檔案名稱進行處理,因為php對中文支援不夠友好【把utf-8 轉為 gb2312】   //1.開啟檔案,先判斷是否存在   if(!file_exists($file_name)){        echo "檔案不存在";         return    ;       }   $fp=fopen($file_name,"r");//以唯讀方式開啟檔案   $file_size=filesize($file_name);   echo "檔案大小:".filesize($file_name);   // 輸入檔案標籤    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))//feof用於判斷檔案是否結束    {      $file_data=fread($fp,$buffer);//讀取資料      $file_count+=$buffer; //統計讀取了多少位元組      //把部分資料回送給瀏覽器      echo $file_data;    }    //關閉檔案    fclose($fp);?>    

 

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.