首頁  >  文章  >  後端開發  >  PHP 弹出文件下载 原理 代码_PHP教程

PHP 弹出文件下载 原理 代码_PHP教程

WBOY
WBOY原創
2016-07-13 10:19:50966瀏覽

PHP 弹出文件下载 原理 代码

/**
 * @author      default7<default7@zbphp.com>
 * @description 演示PHP弹出下载的原理
 *
 * @param $file_name
 */
function downFile($file_name)
{
    $file_path = "/tmp/" . $file_name;
    $buffer = 102400; //一次返回102400个字节
    if (!file_exists($file_path)) {
        echo "<script type=&#39;text/javascript&#39;> alert(&#39;对不起!该文件不存在或已被删除!&#39;); </script>";

        return;
    }
    $fp = fopen($file_path, "r");
    $file_size = filesize($file_path);
    $file_data = &#39;&#39;;
    while (!feof($fp)) {
        $file_data .= fread($fp, $buffer);
    }
    fclose($fp);

    //Begin writing headers
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-type:application/octet-stream;");
    header("Accept-Ranges:bytes");
    header("Accept-Length:{$file_size}");
    header("Content-Disposition:attachment; filename={$file_name}");
    header("Content-Transfer-Encoding: binary");
    echo $file_data;
}


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/871201.htmlTechArticlePHP 弹出文件下载 原理 代码 /** * @author default7 * @description 演示PHP弹出下载的原理 * * @param $file_name */function downFile($file_name){ $file_path = "/tmp/...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn