Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php强制文件下载的自定义函数代码

php强制文件下载的自定义函数代码

发布时间:2018-05-28   编辑:www.jquerycn.cn
本文介绍了php强制文件下载而非在浏览器打开的实例代码,使用php自定义函数实现强制文件下载,有需要的朋友参考下。

有时希望当点击对应链接时直接下载,而不是在网页上显示,那么就需要强制设置header头信息。

分享一段不会产生乱码的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)
 *
 * @return file stream
 */ www.jbxue.com
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增强型mhash函数的实现代码
php强制文件下载的自定义函数代码
PHP 强制下载文件示例代码
php强制下载mp3文件的实现代码
php使用ftp下载文件的简单例子
php ftp下载文件的代码一例
php 强制下载文件实例代码
php强制下载指定类型文件的代码
php ftp文件上传函数的简单例子
php函数 spl_autoload_register的用法详解

关键词: PHP强制下载  自定义函数   
[关闭]