• 技术文章 >后端开发 >php教程

    php导出与下载文件的方法

    2016-07-25 08:54:31原创436
    1. header("Content-Type: application/vnd.ms-excel")
    复制代码

    如果希望能够提供那个打开/保存的对话框,Content-Disposition参数,Content-Disposition参数本来是为了在客户端另存文件时提供一个建议的文件名,但是考虑到安全的原因,就从规范中去掉了这个参数。

    Content-Disposition参数: attachment --- 作为附件下载 inline --- 在线打开

    具体使用:

    1. header("Content-Disposition: inline; filename=文件名.mp3");
    2. Header("Content-Disposition:attachment;filename=test.xls");
    复制代码

    其实IE是根据Content-Disposition中filename这个段中文件名的后缀来识别这个文件类型的,如果有很多种文件类型的时候,可以将Content-Type设置为二进制模式的: Header("Content-type: application/octet-stream");

    例子:

    1. $filename = './download/d.rar ';
    2. $filesize = filesize($filename);
    3. header( "Content-Type: application/force-download ");
    4. header( "Content-Disposition: attachment; filename= ".basename($filename));
    5. header( "Content-Length: ".$filesize);
    6. $data = file_get_contents($filename);
    7. echo $data;
    8. ?>
    复制代码

    以上代码实现打开页面后立即出现下载保存窗口,下载的文件为$filename。

    部分常用mimetype类型:

    $mimetypes = array( 'doc' => 'application/msword', 'bin' => 'application/octet-stream', 'exe' => 'application/octet-stream', 'so' => 'application/octet-stream', 'dll' => 'application/octet-stream', 'pdf' => 'application/pdf', 'ai' => 'application/postscript', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint', 'dir' => 'application/x-director', 'js' => 'application/x-javascript', 'swf' => 'application/x-shockwave-flash', 'xhtml' => 'application/xhtml+xml', 'xht' => 'application/xhtml+xml', 'zip' => 'application/zip', 'mid' => 'audio/midi', 'midi' => 'audio/midi', 'mp3' => 'audio/mpeg', 'rm' => 'audio/x-pn-realaudio', 'rpm' => 'audio/x-pn-realaudio-plugin', 'wav' => 'audio/x-wav', 'bmp' => 'image/bmp', 'gif' => 'image/gif', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'png' => 'image/png', 'css' => 'text/css', 'html' => 'text/html', 'htm' => 'text/html', 'txt' => 'text/plain', 'xsl' => 'text/xml', 'xml' => 'text/xml', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'avi' => 'video/x-msvideo', 'movie' => 'video/x-sgi-movie', );

    >>> 您可能感兴趣的文章: php header函数文件下载时直接提示保存 好用的php header下载函数 php get_headers检测URL是否有效的方法 PHP header函数用法举例 php header函数用法详解举例(2) php header函数用法举例(1) php header 使用详解 php header头信息应用举例 php使用header发送各种类型文件下载的例子 PHP中HEADER头消息详解

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:php导出与下载文件的方法
    上一篇:一个php pdo mysql操作类 下一篇:php访问控制面试题一例
    20期PHP线上班

    相关文章推荐

    精选22门好课,价值3725元,开通VIP免费学习!• 关于整合echop和discuz x的有关问题• 关于怎么获取上拉框循环出来的值的id• php 封锁浏览器程序数据仍有增加• Windows上Php开发环境搭建• php中利用反照访问类私有方法
    1/1

    PHP中文网