设为首页 收藏本站
查看: 402|回复: 0

[经验分享] PHP文件下载过滤类

[复制链接]

尚未签到

发表于 2017-3-28 10:41:17 | 显示全部楼层 |阅读模式
<?php
/**
$filename = 'index.php';
$download = new Download('php,exe,html', false);
if (!$download->downloadfile($filename)) {
echo $download->getErrorMsgs();
}
*/
class Download {
private $debug = false;
private $errorMsg = '';
private $filter = array();
private $fileName = '';
private $mineType = '';
private $xlq_filetype = array();
private $limitTime = 60;
/**
* @param string $fileFilter
* @param boolean $isDebug
*/
function __construct($fileFilter = '', $isDebug = true) {
$this->setFilter($fileFilter);
$this->setDebug($isDebug);
$this->setFileType();
}
function downloadfile($filename) {
$this->fileName = $filename;
if ($this->filecheck()) {
$fn = basename($this->fileName);
ob_end_clean();
@set_time_limit($this->limitTime);
header('Cache-control: max-age=31536000');
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 31536000) . ' GMT');
header('Content-Encoding: none');
header('Content-Length: ' . filesize($this->fileName));
header('Content-Disposition: attachment; filename=' . $fn);
header('Content-Type: ' . $this->mineType);
readfile($this->fileName);
return true;
} else {
return false;
}
}
function filecheck() {
$fileName = $this->fileName;
if (file_exists($fileName)) {
$fileType = strtolower(array_pop(explode(".", $fileName)));
if (!in_array($fileType, $this->filter)) {
$this->errorMsg .= sprintf("%s 不允许下载", $fileName);
if ($this->debug) exit(sprintf("%s 不允许下载", $fileName));
return false;
} else {
if (function_exists("mime_content_type")) {
$this->mineType = mime_content_type($fileName);
}
if (empty($this->mineType)) {
if (isset($this->xlq_filetype[$fileType])) {
$this->mineType = $this->xlq_filetype[$fileType];
}
}
if (!empty($this->mineType)) {
return true;
} else {
$this->errorMsg .= "获取文件类型出错";
if ($this->debug) exit("获取文件类型出错");
return false;
}
}
} else {
$this->errorMsg .= sprintf("%s 不存在", $fileName);
if ($this->debug) exit(sprintf("%s 不存在", $fileName));
return false;
}
}
function setFileType() {
$this->xlq_filetype['chm'] = 'application/octet-stream';
$this->xlq_filetype['ppt'] = 'application/vnd.ms-powerpoint';
$this->xlq_filetype['xls'] = 'application/vnd.ms-excel';
$this->xlq_filetype['doc'] = 'application/msword';
$this->xlq_filetype['exe'] = 'application/octet-stream';
$this->xlq_filetype['rar'] = 'application/octet-stream';
$this->xlq_filetype['js'] = "javascript/js";
$this->xlq_filetype['css'] = "text/css";
$this->xlq_filetype['hqx'] = "application/mac-binhex40";
$this->xlq_filetype['bin'] = "application/octet-stream";
$this->xlq_filetype['oda'] = "application/oda";
$this->xlq_filetype['pdf'] = "application/pdf";
$this->xlq_filetype['ai'] = "application/postsrcipt";
$this->xlq_filetype['eps'] = "application/postsrcipt";
$this->xlq_filetype['es'] = "application/postsrcipt";
$this->xlq_filetype['rtf'] = "application/rtf";
$this->xlq_filetype['mif'] = "application/x-mif";
$this->xlq_filetype['csh'] = "application/x-csh";
$this->xlq_filetype['dvi'] = "application/x-dvi";
$this->xlq_filetype['hdf'] = "application/x-hdf";
$this->xlq_filetype['nc'] = "application/x-netcdf";
$this->xlq_filetype['cdf'] = "application/x-netcdf";
$this->xlq_filetype['latex'] = "application/x-latex";
$this->xlq_filetype['ts'] = "application/x-troll-ts";
$this->xlq_filetype['src'] = "application/x-wais-source";
$this->xlq_filetype['zip'] = "application/zip";
$this->xlq_filetype['bcpio'] = "application/x-bcpio";
$this->xlq_filetype['cpio'] = "application/x-cpio";
$this->xlq_filetype['gtar'] = "application/x-gtar";
$this->xlq_filetype['shar'] = "application/x-shar";
$this->xlq_filetype['sv4cpio'] = "application/x-sv4cpio";
$this->xlq_filetype['sv4crc'] = "application/x-sv4crc";
$this->xlq_filetype['tar'] = "application/x-tar";
$this->xlq_filetype['ustar'] = "application/x-ustar";
$this->xlq_filetype['man'] = "application/x-troff-man";
$this->xlq_filetype['sh'] = "application/x-sh";
$this->xlq_filetype['tcl'] = "application/x-tcl";
$this->xlq_filetype['tex'] = "application/x-tex";
$this->xlq_filetype['texi'] = "application/x-texinfo";
$this->xlq_filetype['texinfo'] = "application/x-texinfo";
$this->xlq_filetype['t'] = "application/x-troff";
$this->xlq_filetype['tr'] = "application/x-troff";
$this->xlq_filetype['roff'] = "application/x-troff";
$this->xlq_filetype['shar'] = "application/x-shar";
$this->xlq_filetype['me'] = "application/x-troll-me";
$this->xlq_filetype['ts'] = "application/x-troll-ts";
$this->xlq_filetype['gif'] = "image/gif";
$this->xlq_filetype['jpeg'] = "image/pjpeg";
$this->xlq_filetype['jpg'] = "image/pjpeg";
$this->xlq_filetype['jpe'] = "image/pjpeg";
$this->xlq_filetype['ras'] = "image/x-cmu-raster";
$this->xlq_filetype['pbm'] = "image/x-portable-bitmap";
$this->xlq_filetype['ppm'] = "image/x-portable-pixmap";
$this->xlq_filetype['xbm'] = "image/x-xbitmap";
$this->xlq_filetype['xwd'] = "image/x-xwindowdump";
$this->xlq_filetype['ief'] = "image/ief";
$this->xlq_filetype['tif'] = "image/tiff";
$this->xlq_filetype['tiff'] = "image/tiff";
$this->xlq_filetype['pnm'] = "image/x-portable-anymap";
$this->xlq_filetype['pgm'] = "image/x-portable-graymap";
$this->xlq_filetype['rgb'] = "image/x-rgb";
$this->xlq_filetype['xpm'] = "image/x-xpixmap";
$this->xlq_filetype['txt'] = "text/plain";
$this->xlq_filetype['c'] = "text/plain";
$this->xlq_filetype['cc'] = "text/plain";
$this->xlq_filetype['h'] = "text/plain";
$this->xlq_filetype['html'] = "text/html";
$this->xlq_filetype['htm'] = "text/html";
$this->xlq_filetype['htl'] = "text/html";
$this->xlq_filetype['rtx'] = "text/richtext";
$this->xlq_filetype['etx'] = "text/x-setext";
$this->xlq_filetype['tsv'] = "text/tab-separated-values";
$this->xlq_filetype['mpeg'] = "video/mpeg";
$this->xlq_filetype['mpg'] = "video/mpeg";
$this->xlq_filetype['mpe'] = "video/mpeg";
$this->xlq_filetype['avi'] = "video/x-msvideo";
$this->xlq_filetype['qt'] = "video/quicktime";
$this->xlq_filetype['mov'] = "video/quicktime";
$this->xlq_filetype['moov'] = "video/quicktime";
$this->xlq_filetype['movie'] = "video/x-sgi-movie";
$this->xlq_filetype['au'] = "audio/basic";
$this->xlq_filetype['snd'] = "audio/basic";
$this->xlq_filetype['wav'] = "audio/x-wav";
$this->xlq_filetype['aif'] = "audio/x-aiff";
$this->xlq_filetype['aiff'] = "audio/x-aiff";
$this->xlq_filetype['aifc'] = "audio/x-aiff";
$this->xlq_filetype['swf'] = "application/x-shockwave-flash";
}
function setFilter($fileFilter) {
if (empty($fileFilter)) return;
$this->filter = explode(",", strtolower($fileFilter));
}
function setDebug($debug) {
$this->debug = $debug;
}
function setlimittime($limittime) {
$this->limitTime = $limittime;
}
function getfilename($filename) {
return $this->fileName;
}
function getErrorMsgs() {
return $this->errorMsg;
}
function __destruct() {
$this->errorMsg = '';
}
}
?>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-356311-1-1.html 上篇帖子: php_验证码__转 下篇帖子: php编程基础(四)数组
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表