php 强制文件下载的一段代码

WBOY
发布: 2016-07-25 09:00:36
原创
659 人浏览过
php实现文件强制下载的一段代码,进入指定页面后就弹出文件下载对话框,恰如梦中那突如其来的流星雨,相当地酷类,哈哈。

php 强制文件下载的代码。

<?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)
 * site http://bbs.it-home.org
 * @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;
    }
}
?>
登录后复制

以上代码,应用的是php头部文件(header)信息的处理方法,如果你曾留心,就会发现程序员之家为大家收集的有关php强制文件下载的代码,大多采用的是这样的方式。



来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!