製作下載按鈕,實現下載功能。

a鏈接如下:

<a href="download.php" target="_self" class="btn btn-success hide" id="btn-download-errorinfo">下载</a>

簡單下載自定義函數:

/**
 * download files
 * $file_path : the file to download
 * $out_filename: 下载显示的文件名称
 */
function _download($file_path,$out_filename="download.log") {
    if(is_file($file_path)) {
        $file = fopen($file_path, "r"); // 打开文件
        if($file) {
            //自定下載顯示義文件名
            $out_filename = str_replace(pathinfo($out_filename,PATHINFO_EXTENSION),'',$out_filename)
                .date("Y.m.d.His")
                .'.'
                .pathinfo($out_filename,PATHINFO_EXTENSION);
       // 输入文件标签
Header("Content-type: application/octet-stream"); Header("Accept-Ranges: bytes"); header('Content-Transfer-Encoding: binary'); Header("Accept-Length: ".filesize($file_path)); //rename Header("Content-Disposition: attachment;filename=".$out_filename); // 输出文件内容 echo fread($file, filesize($file_path)); fclose($file); exit(); }else{ echo false; } }else{ echo false; } }

 

posted @ 2015-01-17 01:07  Zell~Dincht  阅读(200)  评论(0编辑  收藏  举报