// $file = "/folder/filename.ext";
function force_download($file) {
// 截取文件扩展名
$ext = explode(".", $file);
switch($ext[sizeof($ext)-1]) {
case 'jar': $mime = "application/java-archive"; break;
case 'zip': $mime = "application/zip"; break;
case 'jpeg': $mime = "image/jpeg"; break;
case 'jpg': $mime = "image/jpg"; break;
case 'jad': $mime = "text/vnd.sun.j2me.app-descriptor"; break;
case "gif": $mime = "image/gif"; break;
case "png": $mime = "image/png"; break;
case "pdf": $mime = "application/pdf"; break;
case "txt": $mime = "text/plain"; break;
case "doc": $mime = "application/msword"; break;
case "ppt": $mime = "application/vnd.ms-powerpoint"; break;
case "wbmp": $mime = "image/vnd.wap.wbmp"; break;
case "wmlc": $mime = "application/vnd.wap.wmlc"; break;
case "mp4s": $mime = "application/mp4"; break;
case "ogg": $mime = "application/ogg"; break;
case "pls": $mime = "application/pls+xml"; break;
case "asf": $mime = "application/vnd.ms-asf"; break;
case "swf": $mime = "application/x-shockwave-flash"; break;
case "mp4": $mime = "video/mp4"; break;
case "m4a": $mime = "audio/mp4"; break;
case "m4p": $mime = "audio/mp4"; break;
case "mp4a": $mime = "audio/mp4"; break;
case "mp3": $mime = "audio/mpeg"; break;
case "m3a": $mime = "audio/mpeg"; break;
case "m2a": $mime = "audio/mpeg"; break;
case "mp2a": $mime = "audio/mpeg"; break;
case "mp2": $mime = "audio/mpeg"; break;
case "mpga": $mime = "audio/mpeg"; break;
case "wav": $mime = "audio/wav"; break;
case "m3u": $mime = "audio/x-mpegurl"; break;
case "bmp": $mime = "image/bmp"; break;
case "ico": $mime = "image/x-icon"; break;
case "3gp": $mime = "video/3gpp"; break;
case "3g2": $mime = "video/3gpp2"; break;
case "mp4v": $mime = "video/mp4"; break;
case "mpg4": $mime = "video/mp4"; break;
case "m2v": $mime = "video/mpeg"; break;
case "m1v": $mime = "video/mpeg"; break;
case "mpe": $mime = "video/mpeg"; break;
case "mpeg": $mime = "video/mpeg"; break;
case "mpg": $mime = "video/mpeg"; break;
case "mov": $mime = "video/quicktime"; break;
case "qt": $mime = "video/quicktime"; break;
case "avi": $mime = "video/x-msvideo"; break;
case "midi": $mime = "audio/midi"; break;
case "mid": $mime = "audio/mid"; break;
case "amr": $mime = "audio/amr"; break;
default: $mime = "application/force-download";
}
header('Content-Description: File Transfer');
header('Content-Type: '.$mime);
function force_download($file) {
// 截取文件扩展名
$ext = explode(".", $file);
switch($ext[sizeof($ext)-1]) {
case 'jar': $mime = "application/java-archive"; break;
case 'zip': $mime = "application/zip"; break;
case 'jpeg': $mime = "image/jpeg"; break;
case 'jpg': $mime = "image/jpg"; break;
case 'jad': $mime = "text/vnd.sun.j2me.app-descriptor"; break;
case "gif": $mime = "image/gif"; break;
case "png": $mime = "image/png"; break;
case "pdf": $mime = "application/pdf"; break;
case "txt": $mime = "text/plain"; break;
case "doc": $mime = "application/msword"; break;
case "ppt": $mime = "application/vnd.ms-powerpoint"; break;
case "wbmp": $mime = "image/vnd.wap.wbmp"; break;
case "wmlc": $mime = "application/vnd.wap.wmlc"; break;
case "mp4s": $mime = "application/mp4"; break;
case "ogg": $mime = "application/ogg"; break;
case "pls": $mime = "application/pls+xml"; break;
case "asf": $mime = "application/vnd.ms-asf"; break;
case "swf": $mime = "application/x-shockwave-flash"; break;
case "mp4": $mime = "video/mp4"; break;
case "m4a": $mime = "audio/mp4"; break;
case "m4p": $mime = "audio/mp4"; break;
case "mp4a": $mime = "audio/mp4"; break;
case "mp3": $mime = "audio/mpeg"; break;
case "m3a": $mime = "audio/mpeg"; break;
case "m2a": $mime = "audio/mpeg"; break;
case "mp2a": $mime = "audio/mpeg"; break;
case "mp2": $mime = "audio/mpeg"; break;
case "mpga": $mime = "audio/mpeg"; break;
case "wav": $mime = "audio/wav"; break;
case "m3u": $mime = "audio/x-mpegurl"; break;
case "bmp": $mime = "image/bmp"; break;
case "ico": $mime = "image/x-icon"; break;
case "3gp": $mime = "video/3gpp"; break;
case "3g2": $mime = "video/3gpp2"; break;
case "mp4v": $mime = "video/mp4"; break;
case "mpg4": $mime = "video/mp4"; break;
case "m2v": $mime = "video/mpeg"; break;
case "m1v": $mime = "video/mpeg"; break;
case "mpe": $mime = "video/mpeg"; break;
case "mpeg": $mime = "video/mpeg"; break;
case "mpg": $mime = "video/mpeg"; break;
case "mov": $mime = "video/quicktime"; break;
case "qt": $mime = "video/quicktime"; break;
case "avi": $mime = "video/x-msvideo"; break;
case "midi": $mime = "audio/midi"; break;
case "mid": $mime = "audio/mid"; break;
case "amr": $mime = "audio/amr"; break;
default: $mime = "application/force-download";
}
header('Content-Description: File Transfer');
header('Content-Type: '.$mime);
//文件名要加上引号不然文件名有空格的时候会有问题
header('Content-Disposition: attachment; filename="'.basename($file).'"');
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($file));
ob_clean();
flush();
readfile($file);
}
header('Content-Disposition: attachment; filename="'.basename($file).'"');
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($file));
ob_clean();
flush();
readfile($file);
}
相关推荐
首先,我们来理解一下PHP文件下载的基本原理。当用户点击下载链接时,服务器会发送一个HTTP响应给客户端(通常是浏览器),这个响应包含了文件的内容和一些HTTP头信息,比如Content-Type定义了文件的MIME类型,...
有时候网站需要下载文件,这里献上一个PHP文件下载功能,内付教程,简单实用。
PHP文件下载类就是为了解决这个问题而设计的,它封装了处理文件下载所需的核心功能,使得开发者可以更方便、高效地实现文件的下载服务。下面将详细阐述PHP文件下载的原理以及如何使用此类。 首先,理解PHP是如何...
总的来说,这个PHP文件下载类提供了灵活的下载控制,包括限速和断点续传,为服务器上的文件下载提供了高效且友好的解决方案。通过此类,开发者可以根据实际需求定制下载体验,提高用户的满意度。
这个名为"jquery+ajax+php文件下载.rar"的压缩包文件很可能包含了一个示例项目,展示了如何利用这些技术实现在网页上无刷新地下载文件。下面我们将详细探讨这三个关键知识点。 首先,jQuery是一个广泛使用的...
这里我们关注的焦点是“基于HTTP PHP文件下载类”。这个压缩包文件包含了一个名为`httpdownload.class.php`的文件,这通常是一个实现了文件下载功能的PHP类。下面我们将详细探讨这个类的工作原理以及相关的PHP知识。...
"PHP文件下载过滤类"是指一个专门设计用于控制和管理文件下载的安全类,它可以防止非法或者恶意的下载请求,确保只有合法的文件能被用户访问。这个类通常会包含一系列的验证和过滤机制,如检查文件路径的合法性、...
本文将详细介绍一个PHP文件下载类及其用法,帮助你理解如何在自己的项目中实现类似功能。 首先,`download_file.php` 文件可能包含了下载类的定义。此类通常包含以下几个核心方法: 1. `setFilePath()`:设置要...
php文件下载, //告诉浏览器返回的文件 header("Content_type:application/octet-stream"); //以字节大小返回 header("Accept-Ranges:bytes"); //返回文件大小 header("Accept-Length:$file_size"); /...
PHP文件下载类,可以直接调用,操作文件下载
适合新手学习的PHP文件下载源代码,代码功能简单,有详细的注释,新手学习的话可以看看,老手绕道
"PHP文件下载,开箱即用"这个标题暗示我们将讨论如何通过简单的PHP代码来创建一个能够立即使用的文件下载功能。下面,我们将深入探讨PHP文件下载的核心概念、实现方法以及相关注意事项。 一、PHP文件下载原理 PHP...
PHP实现统计文件下载次数 一、开发环境 1、环境搭建:Windows 8+Apache 2.4.18+MySQL 5.7.11+PHP 7.1.0 。 2、文本编辑器:Sublime Text3。 二、主要技术 本实验主要使用PHP、Mysql和jQuery实现文件下载次数统计。
// $download=new download('php,exe,html',false); // if(!$download->downloadfile($filename)) // { // echo $download->geterrormsg(); // }
实现文件安全下载,完成我文件下载的安全性,并限制用户的下载次数。
PHP文件下载的函数,能扩展,一起学习进步吧!
sphinx搜索引擎下面对php的扩展sphinxapi.php文件sphinx搜索引擎下面对php的扩展sphinxapi.php文件sphinx搜索引擎下面对php的扩展sphinxapi.php文件
根据提供的文件信息,我们可以深入探讨PHP文件流下载的相关知识点,包括其原理、实现方式以及应用场景等。 ### PHP文件流下载的基本概念 文件流下载是指在Web应用中将服务器上的文件发送到客户端(如浏览器),供...
在PHP中实现浏览器访问地址栏下载文件的功能是一项常见的任务,主要涉及到HTTP协议的响应头设置以及文件内容的读取和输出。在这个项目中,我们有两个关键文件:`2017030615453109.pdf` 和 `getpdf.php`。下面我们将...
1. **PHP文件下载**: PHP可以通过`header()`函数来设置HTTP响应头,从而触发浏览器进行文件下载。例如,可以使用`header('Content-Type: application/octet-stream');`来指定文件类型,`header('Content-...