龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

文件下载 PHP 强制文件下载

时间:2014-06-19 02:17来源:网络整理 作者:网络 点击:
分享到:
PHP 强制文件下载 进入指定页面后就弹出文件下载对话框**[PHP]代码**```{.php}/** * Downloader * * @param $archivo * path al archivo * @param $downloadfilename * (null|string) el nombre que queres usar para el arc

进入指定页面后就弹出文件下载对话框

[PHP]代码

/**
 * Downloader
 *
 * @param $archivo
 *  path al archivo
 * @param $downloadfilename
 *  (null|string) el nombre que queres usar para el archivo que se va a descargar.
 *  (si no lo especificas usa el nombre actual del archivo)
 *
 * @return file stream
 */
function download_file($archivo, $downloadfilename = null) {

    if (file_exists($archivo)) {
        $downloadfilename = $downloadfilename !== null ? $downloadfilename : basename($archivo);
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . $downloadfilename);
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($archivo));

        ob_clean();
        flush();
        readfile($archivo);
        exit;
    }

}
精彩图集

赞助商链接