PHP(http協議)-檔案下載

來源:互聯網
上載者:User
<?php    //檔案下載,下載一張圖片    //$file_name="Angel.mp3";    $file_name="小丸子.jpg";  //出現中文 程式無法完成下載 提示:檔案不存在     //對檔案進行轉碼(PHP檔案函數 比較古老 需對中文碼轉成 gb2312)    //iconv — Convert string to requested character encoding    $file_name=iconv("utf-8","gb2312",$file_name);    //設定檔案下載路徑(相對路徑)    //$file_path="./dowm/".$file_name;    //使用絕對路徑    $file_path=$_SERVER['DOCUMENT_ROOT']."/http/dowm/".$file_name;    //開啟檔案---先判斷再操作    if(!file_exists($file_path)){                echo "檔案不存在";        return ;    //直接退出    }    //存在--開啟檔案    $fp=fopen($file_path,"r");        //擷取檔案大小    $file_size=filesize($file_path);    //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);?>

 2、封裝函數:

<?php        /*        封裝函數:        參數說明----$file_name:檔案名稱                    $file_sub_dir:檔案下載的子路徑     */    function file_dowm($file_name,$file_sub_dir){        //檔案轉碼        $file_name=iconv("utf-8","gb2312",$file_name);        //使用絕對路徑        $file_path=$_SERVER['DOCUMENT_ROOT']."$file_sub_dir".$file_name;        //開啟檔案---先判斷再操作        if(!file_exists($file_path)){                        echo "檔案不存在";            return ;    //直接退出        }        //存在--開啟檔案        $fp=fopen($file_path,"r");                //擷取檔案大小        $file_size=filesize($file_path);        /*        //這裡可以設定超過多大不能下載        if($file_size>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("小丸子.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.