首页 > 代码库 > 文件下载类大全
文件下载类大全
<?php if (!defined(‘BASEPATH‘)) exit(‘No direct script access allowed‘);/* $download = new download(‘php,exe,html‘,false);$this->load->library(‘Download‘);if(!$this->download->->downloadfile($filename)){ echo $this->download->geterrormsg();}*/ class Download{ var $debug=true; var $errormsg=‘‘; var $Filter=array(); var $filename=‘‘; var $_realname = ‘‘; var $mineType=‘text/plain‘; var $filetype =‘‘; var $xlq_filetype=array(); protected $CI = NULL; function __construct($fileFilter=‘‘,$isdebug=false) { $this->CI = &get_instance(); $this->setFilter($fileFilter); $this->setdebug($isdebug); $this->setfiletype(); } function setFilter($fileFilter) { if(empty($fileFilter)) return ; $this->Filter=explode(‘,‘,strtolower($fileFilter)); } function setdebug($debug) { $this->debug=$debug; } function setfilename($filename,$_realname) { $this->filename = mb_convert_encoding(trim($filename),"Big5"); $this->_realname = empty($_realname) ? $this->filename : $_realname; } function downloadfile($filename,$_realname=‘‘,$schdule=false) { $this->setfilename($filename,$_realname); if($this->filecheck($schdule)) { //重新設置文件名稱 edited by zcs $fn = !empty($_realname) ? $this->trim_special_str($_realname) : array_pop( explode( ‘/‘, strtr( $this->filename, ‘\\‘, ‘/‘ ) ) ); if(!empty($_realname) && strtolower(array_pop(explode(‘.‘,$fn))) !== $this->filetype){ $fn .= ‘.‘.$this->filetype; } $this->filename = str_replace(‘ ‘,‘%20‘, $this->filename); header( "Content-type: text/plain; charset=utf-8"); header( "Pragma: public" ); header( "Expires: 0" ); // set expiration time header( "Cache-Component: must-revalidate, post-check=0, pre-check=0" ); header( "Content-type:".$this->mineType ); if(!$schdule){ header( "Content-Length: " . get_file_size( $this->filename ) ); } if(preg_match("/MSIE/i", $_SERVER["HTTP_USER_AGENT"])){ header( ‘Content-Disposition: attachment; filename="‘.urlencode($fn).‘"‘ );//解决IE中文乱码 }else{ header( ‘Content-Disposition: attachment; filename="‘.$fn.‘"‘ );//解决IE中文乱码 } header( ‘Content-Transfer-Encoding: binary‘ ); readfile( $this->filename ); return true; } else { return false; } } //add by zcs function trim_special_str($_str) { return str_replace(array(‘/‘,‘?‘,‘*‘,‘:‘,‘"‘,‘<‘,‘>‘,‘\\‘),array(‘‘,‘‘,‘‘,‘‘,‘‘,‘‘,‘‘,‘‘),$_str); } function geterrormsg() { return $this->errormsg; } function filecheck($schdule) { $filename = $this->filename; $lang_test = $this->CI->lang->language; header( "Content-type: text/html; charset=utf-8"); if(es_file_exists($filename)) { $this->filetype=strtolower(array_pop(explode(‘.‘,$filename))); if(in_array($this->filetype,$this->Filter)) { $this->errormsg.=$filename.$lang_test[‘file_forbbing_down‘]; if($this->debug) exit($filename.$lang_test[‘file_forbbing_down‘]) ; return false; }else { if(!$schdule){ $this->file_mime_type($filename); }else{ $this->mineType = ‘application/msword‘; } if(empty($this->mineType)) { $this->mineType = isset($this->xlq_filetype[$this->filetype]) ? $this->xlq_filetype[$this->filetype] : ‘application/octet-stream‘; } if(!empty($this->mineType)) return true; else { $this->errormsg.= $filename.$lang_test[‘get_file_type_error‘]; if($this->debug) exit($lang_test[‘get_file_type_error‘]); return false; } } }else { $filename = iconv("Big5","UTF-8",trim($filename));//防止乱码 $this->errormsg.=$filename.$lang_test[‘file_not_found‘]; if($this->debug) exit($filename.$lang_test[‘file_not_found‘]) ; return false; } } function setfiletype() { $this->xlq_filetype[‘chm‘]=‘application/octet-stream‘; $this->xlq_filetype[‘ppt‘]=‘application/vnd.ms-powerpoint‘; $this->xlq_filetype[‘odp‘]=‘application/vnd.ms-powerpoint‘; $this->xlq_filetype[‘xls‘]=‘application/vnd.ms-excel‘; $this->xlq_filetype[‘doc‘]=‘application/msword‘; $this->xlq_filetype[‘pptx‘]=‘application/vnd.ms-powerpoint‘; $this->xlq_filetype[‘pps‘]=‘application/vnd.ms-powerpoint‘; $this->xlq_filetype[‘xlsx‘]=‘application/vnd.ms-excel‘; $this->xlq_filetype[‘docx‘]=‘application/msword‘; $this->xlq_filetype[‘odt‘]=‘application/msword‘; $this->xlq_filetype[‘ods‘]=‘application/vnd.ms-excel‘; $this->xlq_filetype[‘ws‘]=‘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 file_mime_type($file) { $regexp = ‘/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/‘; if (function_exists(‘finfo_file‘)) { $finfo = finfo_open(FILEINFO_MIME); if (is_resource($finfo)){ $mime = @finfo_file($finfo, $file); finfo_close($finfo); if (is_string($mime) && preg_match($regexp, $mime, $matches)){ $this->mineType = $matches[1]; return; } } } if (DIRECTORY_SEPARATOR !== ‘\\‘){ $cmd = ‘file --brief --mime ‘ . escapeshellarg($file) . ‘ 2>&1‘; if (function_exists(‘exec‘)){ $mime = @exec($cmd, $mime, $return_status); if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches)){ $this->mineType = $matches[1]; return; } } if ( (bool) @ini_get(‘safe_mode‘) === FALSE && function_exists(‘shell_exec‘)){ $mime = @shell_exec($cmd); if(strlen($mime) > 0){ $mime = explode("\n", trim($mime)); if (preg_match($regexp, $mime[(count($mime) - 1)], $matches)){ $this->mineType = $matches[1]; return; } } } if (function_exists(‘popen‘)){ $proc = @popen($cmd, ‘r‘); if (is_resource($proc)){ $mime = @fread($proc, 512); @pclose($proc); if ($mime !== FALSE){ $mime = explode("\n", trim($mime)); if (preg_match($regexp, $mime[(count($mime) - 1)], $matches)){ $this->mineType = $matches[1]; return; } } } } } // Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field][‘type‘]) if (function_exists(‘mime_content_type‘)){ $this->mineType = @mime_content_type($file); if (strlen($this->mineType) > 0){ return; } } }}?>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。