下载文件总结
生成 HTTP 头强制下载数据到客户端,这在实现文件下载时很有用。
首先感谢@yangweijie的作品,在此基础上衍生出不需要开启php_fileinfo.dll扩展的方法。
方法一:开启扩展情况下//$file 文件路径<br>
public function download_file($file){<br>
if(is_file($file)){<br>
$length = filesize($file);<br>
$type = mime_content_type($file);<br>
$showname = ltrim(strrchr($file,'/'),'/');<br>
header("Content-Description: File Transfer");<br>
header('Content-type: ' . $type);<br>
header('Content-Length:' . $length);<br>
if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE<br>
header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');<br>
} else {<br>
header('Content-Disposition: attachment; filename="' . $showname . '"');<br>
}<br>
readfile($file);<br>
} else {<br>
$this->error('源文件不存在!');<br>
}<br>
}
方法二,如果没有开启php_fileinfo.dll,谁让咱是没有服务器权限的程序员呢,又不能让人家看不起,唉!可能你会懂。public function force_download($filename)<br>
{<br>
if ($filename == ''){<br>
return FALSE;<br>
}<br>
if (FALSE === strpos($filename, '.')){<br>
return FALSE;<br>
}<br>
$x = explode('.', $filename);<br>
$extension = end($x);<br>
$mimes =$this->getMimes();<br>
<br>
// Set a default mime if we can't find it<br>
if ( ! isset($mimes[$extension])){<br>
$mime = 'application/octet-stream';<br>
}else{<br>
$mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];<br>
}<br>
// Generate the server headers<br>
if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)<br>
{<br>
header('Content-Type: "'.$mime.'"');<br>
header('Content-Disposition: attachment; filename="'.$filename.'"');<br>
header('Expires: 0');<br>
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');<br>
header("Content-Transfer-Encoding: binary");<br>
header('Pragma: public');<br>
header("Content-Length: ".filesize($filename));<br>
}<br>
else<br>
{<br>
header('Content-Type: "'.$mime.'"');<br>
header('Content-Disposition: attachment; filename="'.$filename.'"');<br>
header("Content-Transfer-Encoding: binary");<br>
header('Expires: 0');<br>
header('Pragma: no-cache');<br>
header("Content-Length: ".filesize($filename));<br>
}<br>
readfile($filename);<br>
}<br>
private function getMimes(){<br>
return $mimes = array( 'hqx' => 'application/mac-binhex40',<br>
'cpt' => 'application/mac-compactpro',<br>
'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'),<br>
'bin' => 'application/macbinary',<br>
'dms' => 'application/octet-stream',<br>
'lha' => 'application/octet-stream',<br>
'lzh' => 'application/octet-stream',<br>
'exe' => array('application/octet-stream', 'application/x-msdownload'),<br>
'class' => 'application/octet-stream',<br>
'psd' => 'application/x-photoshop',<br>
'so' => 'application/octet-stream',<br>
'sea' => 'application/octet-stream',<br>
'dll' => 'application/octet-stream',<br>
'oda' => 'application/oda',<br>
'pdf' => array('application/pdf', 'application/x-download'),<br>
'ai' => 'application/postscript',<br>
'eps' => 'application/postscript',<br>
'ps' => 'application/postscript',<br>
'smi' => 'application/smil',<br>
'smil' => 'application/smil',<br>
'mif' => 'application/vnd.mif',<br>
'xls' => array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'),<br>
'ppt' => array('application/powerpoint', 'application/vnd.ms-powerpoint'),<br>
'wbxml' => 'application/wbxml',<br>
'wmlc' => 'application/wmlc',<br>
'dcr' => 'application/x-director',<br>
'dir' => 'application/x-director',<br>
'dxr' => 'application/x-director',<br>
'dvi' => 'application/x-dvi',<br>
'gtar' => 'application/x-gtar',<br>
'gz' => 'application/x-gzip',<br>
'php' => 'application/x-httpd-php',<br>
'php4' => 'application/x-httpd-php',<br>
'php3' => 'application/x-httpd-php',<br>
'phtml' => 'application/x-httpd-php',<br>
'phps' => 'application/x-httpd-php-source',<br>
'js' => 'application/x-javascript',<br>
'swf' => 'application/x-shockwave-flash',<br>
'sit' => 'application/x-stuffit',<br>
'tar' => 'application/x-tar',<br>
'tgz' => array('application/x-tar', 'application/x-gzip-compressed'),<br>
'xhtml' => 'application/xhtml+xml',<br>
'xht' => 'application/xhtml+xml',<br>
'zip' => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'),<br>
'mid' => 'audio/midi',<br>
'midi' => 'audio/midi',<br>
'mpga' => 'audio/mpeg',<br>
'mp2' => 'audio/mpeg',<br>
'mp3' => array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'),<br>
'aif' => 'audio/x-aiff',<br>
'aiff' => 'audio/x-aiff',<br>
'aifc' => 'audio/x-aiff',<br>
'ram' => 'audio/x-pn-realaudio',<br>
'rm' => 'audio/x-pn-realaudio',<br>
'rpm' => 'audio/x-pn-realaudio-plugin',<br>
'ra' => 'audio/x-realaudio',<br>
'rv' => 'video/vnd.rn-realvideo',<br>
'wav' => array('audio/x-wav', 'audio/wave', 'audio/wav'),<br>
'bmp' => array('image/bmp', 'image/x-windows-bmp'),<br>
'gif' => 'image/gif',<br>
'jpeg' => array('image/jpeg', 'image/pjpeg'),<br>
'jpg' => array('image/jpeg', 'image/pjpeg'),<br>
'jpe' => array('image/jpeg', 'image/pjpeg'),<br>
'png' => array('image/png', 'image/x-png'),<br>
'tiff' => 'image/tiff',<br>
'tif' => 'image/tiff',<br>
'css' => 'text/css',<br>
'html' => 'text/html',<br>
'htm' => 'text/html',<br>
'shtml' => 'text/html',<br>
'txt' => 'text/plain',<br>
'text' => 'text/plain',<br>
'log' => array('text/plain', 'text/x-log'),<br>
'rtx' => 'text/richtext',<br>
'rtf' => 'text/rtf',<br>
'xml' => 'text/xml',<br>
'xsl' => 'text/xml',<br>
'mpeg' => 'video/mpeg',<br>
'mpg' => 'video/mpeg',<br>
'mpe' => 'video/mpeg',<br>
'qt' => 'video/quicktime',<br>
'mov' => 'video/quicktime',<br>
'avi' => 'video/x-msvideo',<br>
'movie' => 'video/x-sgi-movie',<br>
'doc' => 'application/msword',<br>
'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'),<br>
'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'),<br>
'word' => array('application/msword', 'application/octet-stream'),<br>
'xl' => 'application/excel',<br>
'eml' => 'message/rfc822',<br>
'json' => array('application/json', 'text/json')<br>
);
看到这里估计大家都懂了,就是由后缀去匹配文件类型。实现mime_content_type的功能。
【广告】
本人承接商城,办公,企业站,分销等系统开发及二次开发。欢迎咨询。

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

了解Python编程的入门级代码示例Python是一种简单易学,功能强大的编程语言。对于初学者来说,了解Python编程的入门级代码示例是非常重要的。本文将为您提供一些具体的代码示例,帮助您快速入门。打印HelloWorldprint("HelloWorld")这是Python中最简单的代码示例。print()函数用于将指定的内容输出

PHP变量存储程序运行期间的值,对于构建动态且交互式的WEB应用程序至关重要。本文将深入探讨php变量,并通过10个真实的示例展示它们的实际应用。1.存储用户输入$username=$_POST["username"];$passWord=$_POST["password"];此示例从表单提交中提取用户名和密码,并将其存储在变量中以供进一步处理。2.设置配置值$database_host="localhost";$database_username="username";$database_pa

《Go语言编程实例:Web开发中的代码示例》随着互联网的快速发展,Web开发已经成为各行业中必不可少的一部分。作为一门功能强大且性能优越的编程语言,Go语言在Web开发中越来越受到开发者们的青睐。本文将通过具体的代码示例,介绍如何利用Go语言进行Web开发,让读者能够更好地理解和运用Go语言来构建自己的Web应用。1.简单的HTTP服务器首先,让我们从一个

标题:从入门到精通:Go语言中常用数据结构的代码实现数据结构在编程中起着至关重要的作用,它是程序设计的基础。在Go语言中,有许多常用的数据结构,掌握这些数据结构的实现方式对于成为一名优秀的程序员至关重要。本文将介绍Go语言中常用的数据结构,并给出相应的代码示例,帮助读者从入门到精通这些数据结构。1.数组(Array)数组是一种基本的数据结构,是一组相同类型

Java冒泡排序最简单的代码示例冒泡排序是一种常见的排序算法,它的基本思想是通过相邻元素的比较和交换来将待排序序列逐步调整为有序序列。下面是一个简单的Java代码示例,演示了如何实现冒泡排序:publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

华为云边缘计算对接指南:Java代码示例快速实现接口随着物联网技术的快速发展和边缘计算的兴起,越来越多的企业开始关注边缘计算的应用。华为云提供了边缘计算服务,为企业提供了高可靠的计算资源和便捷的开发环境,使得边缘计算应用更加容易实现。本文将介绍如何通过Java代码快速实现华为云边缘计算的接口。首先,我们需要准备好开发环境。确保你已经安装了Java开发工具包(

Java选择排序法代码编写指南及示例选择排序是一种简单直观的排序算法,其思想是每次从未排序的元素中选择最小(或最大)的元素进行交换,直到所有元素排序完成。本文将提供选择排序的代码编写指南,并附上具体的Java示例代码。算法原理选择排序的基本原理是将待排序数组分为已排序和未排序两部分,每次从未排序部分选择最小(或最大)的元素,将其放到已排序部分的末尾。重复上述

如何使用PHP编写库存管理系统中的库存分仓管理功能代码库存管理是许多企业中不可或缺的一部分。对于拥有多个仓库的企业来说,库存分仓管理功能尤为重要。通过合理管理和跟踪库存,企业可以实现不同仓库之间的库存调拨,优化运营成本,改善协同效率。本文将介绍如何使用PHP编写库存分仓管理功能的代码,并为您提供相关的代码示例。一、建立数据库在开始编写库存分仓管理功能的代码之
