php 文件下载方法

用的是表头方式下载

 代码如下 复制代码
Header( "Content-type:   application/octet-stream ");
Header( "Accept-Ranges:   bytes ");
Header( "Accept-Length:   ".filesize($file_dir   .   $file_name));
Header( "Content-Disposition:   attachment;   filename= "   .   $file_name);

判断文件存在再下载

 代码如下 复制代码
<?php
    if( isset( $_REQUEST["download"] ) )
    {
            $tfile = $_REQUEST["download"];
           
            if (file_exists($tfile))
            {
                $downfilename=substr($tfile,strrpos($tfile, "/")+1);
               //文件保存的名字可以修改为你需要的,可以和链接给的名字不一样
                // Prompt the user to download the new torrent file.
                header( "Content-type: application/octet-streamn" );
                header( "Content-disposition: attachment; filename=$downfilenamen" );
                header( "Content-transfer-encoding: binaryn");
                header( "Content-length: " . @filesize($tfile ) . "n" );
                // Send the torrent file
                $fp = @fopen( $tfile, "r" );
                @fpassthru( $fp );
                @fclose( $fp );
             }
           
            exit();
     }
?>

pdf文件下载

 代码如下 复制代码

<?php
$filename = $_SERVER['DOCUMENT_ROOT'] . "/path/to/file/my_file.pdf";
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($filename));
header("Content-Type: application/pdf"); //pdf格式的

?>

下面讲讲header中的Content-type:
不同的下载文件,对应不同的content-type,下面是大全:
PHP code
{
'ez' => 'application/andrew-inset',
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'doc' => 'application/msword',
'bin' => 'application/octet-stream',
'dms' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'class' => 'application/octet-stream',
'so' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'oda' => 'application/oda',
'pdf' => 'application/pdf',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'smi' => 'application/smil',
'smil' => 'application/smil',
'mif' => 'application/vnd.mif',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'wbxml' => 'application/vnd.wap.wbxml',
'wmlc' => 'application/vnd.wap.wmlc',
'wmlsc' => 'application/vnd.wap.wmlscriptc',
'bcpio' => 'application/x-bcpio',
'vcd' => 'application/x-cdlink',
'pgn' => 'application/x-chess-pgn',
'cpio' => 'application/x-cpio',
'csh' => 'application/x-csh',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'dvi' => 'application/x-dvi',
'spl' => 'application/x-futuresplash',
'gtar' => 'application/x-gtar',
'hdf' => 'application/x-hdf',
'js' => 'application/x-javascript',
'skp' => 'application/x-koan',
'skd' => 'application/x-koan',
'skt' => 'application/x-koan',
'skm' => 'application/x-koan',
'latex' => 'application/x-latex',
'nc' => 'application/x-netcdf',
'cdf' => 'application/x-netcdf',
'sh' => 'application/x-sh',
'shar' => 'application/x-shar',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'sv4cpio' => 'application/x-sv4cpio',
'sv4crc' => 'application/x-sv4crc',
'tar' => 'application/x-tar',
'tcl' => 'application/x-tcl',
'tex' => 'application/x-tex',
'texinfo' => 'application/x-texinfo',
'texi' => 'application/x-texinfo',
't' => 'application/x-troff',
'tr' => 'application/x-troff',
'roff' => 'application/x-troff',
'man' => 'application/x-troff-man',
'me' => 'application/x-troff-me',
'ms' => 'application/x-troff-ms',
'ustar' => 'application/x-ustar',
'src' => 'application/x-wais-source',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'zip' => 'application/zip',
'au' => 'audio/basic',
'snd' => 'audio/basic',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'kar' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'm3u' => 'audio/x-mpegurl',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'wav' => 'audio/x-wav',
'pdb' => 'chemical/x-pdb',
'xyz' => 'chemical/x-xyz',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'ief' => 'image/ief',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'djvu' => 'image/vnd.djvu',
'djv' => 'image/vnd.djvu',
'wbmp' => 'image/vnd.wap.wbmp',
'ras' => 'image/x-cmu-raster',
'pnm' => 'image/x-portable-anymap',
'pbm' => 'image/x-portable-bitmap',
'pgm' => 'image/x-portable-graymap',
'ppm' => 'image/x-portable-pixmap',
'rgb' => 'image/x-rgb',
'xbm' => 'image/x-xbitmap',
'xpm' => 'image/x-xpixmap',
'xwd' => 'image/x-xwindowdump',
'igs' => 'model/iges',
'iges' => 'model/iges',
'msh' => 'model/mesh',
'mesh' => 'model/mesh',
'silo' => 'model/mesh',
'wrl' => 'model/vrml',
'vrml' => 'model/vrml',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'asc' => 'text/plain',
'txt' => 'text/plain',
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'sgml' => 'text/sgml',
'sgm' => 'text/sgml',
'tsv' => 'text/tab-separated-values',
'wml' => 'text/vnd.wap.wml',
'wmls' => 'text/vnd.wap.wmlscript',
'etx' => 'text/x-setext',
'xsl' => 'text/xml',
'xml' => 'text/xml',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'mxu' => 'video/vnd.mpegurl',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'ice' => 'x-conference/x-cooltalk',
}

时间: 2024-09-28 16:30:33

php 文件下载方法的相关文章

jsp文件下载方法

<%  response.setContentType(fileminitype);  response.setHeader("Location",filename);  response.setHeader("Cache-Control", "max-age=" + cacheTime);  response.setHeader("Content-Disposition", "attachment; file

java阻止浏览打开文件下载方法

很简单的只要在在web.xml文件中配置mime下载文件类型就可以了 下面提供一个配置参数文档. <mime-mapping> <extension>doc</extension> <mime-type>application/msword</mime-type> </mime-mapping> <extension>chm</extension> <mime-type>application/ms

android中集中html5,怎样实现文件下载功能,我现在有一个超链接

问题描述 android中集中html5,怎样实现文件下载功能,我现在有一个超链接 <a href="http://xxxxxxx/group1/M00/00/08/Ci09DVSb9n6AH76IAAX4AIw6xuk01.docx?attname=基于ANT的增量代码检查.docx&attachid=942'" download="基于ANT的增量代码检查.docx" class="graybtn">下载</a>

Flash中表现粗细变化笔触效果

本文主要介绍在Flash中用鼠标模拟压感笔有粗细变化的笔触效果的方法. 我们知道,压感笔是专业图形设计师常用的一种辅助设计工具.设计师们可以利用它的压力感知功能用手轻松的描绘出笔画的粗细.色调的明暗以及色彩的强度.特别是笔画粗细不同的笔触效果,看起来自然而流畅,绘制出的各类形象个性鲜明.独具风格(见下图图解). 在许多flash动画作品中,我们都能看到这样的笔触效果,这些作品通常都是利用压感笔来完成的.然而,对于我们大多数的普通用户而言,手边往往都只有个小鼠标,根本没有配备压感笔.那么我们有没有

Nodejs实现批量下载妹纸图

  这篇文章主要介绍了使用Nodejs实现批量下载妹纸图的方法和详细代码,十分的实用,喜欢妹纸的小伙伴们可以参考下. 听说最近下载妹子图很火? Nodejs (javascrpt)自然不能落后~ 虽然从没写过像样的Nodejs程序,但作为至少翻过书的前端同学来说,Nodejs用得还蛮顺手的哈~ 花了一点事件学习了下Nodejs的网页获取和文件下载方法,没事乱捣腾就写了这个半成品的下载器 使用方法: 1)新建一个download目录 2)新建download.js(其实名字随便取),并复制到dow

Beyondsearch怎么用

  1.下载beyondsearcher 2.双击"beyondsearcher"程序图标,运行"beyondsearcher"程序,在beyondsearcher搜索框中输入要搜索的内容,点击"搜索"按钮就正式进入搜索过程. 3.文件下载方法:首先选择一种下载方式,在此选择"迅雷"下载,然后勾选要下载的文件,右击从弹出的菜单中选择"下载选中文件"项. 4.然后就会自动启用相应的下载程序进行文件的下载操作.

Gracejs : 全新的基于koa2的前后端分离框架

Gracejs(又称:koa-grace v2) 是全新的基于koa v2.x的MVC+RESTful架构的前后端分离框架. 一.简介 Gracejs是koa-grace的升级版,也可以叫koa-grace v2. github地址: https://github.com/xiongwilee/koa-grace. 主要特性包括: 支持MVC架构,可以更便捷地生成服务端路由; 标准的RESTful架构,支持后端接口异步并发,页面性能更优; 一套Node环境经服务服务多个站点应用,部署更简单; 优

RDS for MySQL 备份文件恢复到本地数据库

干货就是直接上话,上图,上流程.用官方的备份方案麻烦 安装percona-Xtrabackup数据恢复软件进行数据恢复,而且没有详细的图,和前期说明 RDS Mysql环境:MySQL 5.6, 本机环境:centos6.5,mysql5.6 1.centos6.5的源是5.1版本所以使用rpm进行包安装,去官网下载https://dev.mysql.com/downloads/file/?id=450542 2.下载完成之后使用 yum localinstall mysql-community

Asp.net(c#)常用文件操作类封装 移动 复制 删除 上传 下载等

Asp.net(c#)中常用文件操作类封装 包括:移动 复制 删除 上传 下载等 using System; using System.Configuration; using System.Data; using System.IO; using System.Text; using System.Threading; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.Ht