PHP5+UTF8多文件上传类
还有些功能没有加上去,如自动更名,图片处理等.可根据需要自己添加.
use:
$up = new upfile(root_path.'data/'.date("ym",time()),array('gif','jpg','jpeg'),true);
$fileimg = $up->upload($_files['img']);//返回上传后文件名数组,$_files['img']为上传的文件
可使用$up->log查看上传时信息.
<?php
//====================================================
// filename: upfile.class.php
// summary: 文件上传类
// author: millken(迷路林肯)
// lastmodifed: 2008-6-4
// copyright (c)2008 millken@gmail.com
//====================================================
if(!defined('ok'))exit(__file__.'access denied');
class upfile {
public $extensionfileformat = array();
public $returninfo = array();
private $imagefileformat = array('gif','bmp','jpg','jpe','jpeg','png');
private $otherfileformat = array('zip','pdf','rar','xls','doc','ppt','csv');
private $savepath;
private $attachment_path = './upfiles/';
private $overwrite = false; # 同名时是否覆盖
private $maxsize = 0; # 文件最大字节,为0时不限制大小
private $ext;
private $errno = 0;
/* 构造函数
* (string)$savepath 文件保存路径,默认为$attachment_path
* (array)$extensionfileformat 自定义上传文件的扩展名,未设置时为$imagefileformat || $otherfileformat
* (bool)$overwrite 是否覆盖同名文件
*/
public function __construct($savepath='',$extensionfileformat = array(),$overwrite = false) {
$this->savepath = empty($savepath)?$this->attachment_pathsavepath.'/';
$this->extensionfileformat = is_array($extensionfileformat)?$extensionfileformat:array();
$this->overwrite = is_bool($overwrite)?$overwrite:false;
}
/*上传函数
* (array)$files 待上传的文件数组$_files['attach']
* (number)$maxsize 文件的最大字节数,默认为0不限制上传大小
*/
public function upload($files,$maxsize=0) {
$this->maxsize = is_numeric($maxsize)?$maxsize:0;
if(isset($files) && is_array($files)) {
if(is_array($files['name'])) {
foreach($files as $key => $var) {
foreach($var as $id => $val) {
$attachments[$id][$key] = $val;
}
}
} else {
$attachments[] = $files;
}
}
self::check_file_type($attachments);
if(empty($this->filelist)) {
$this->log .= "待上传的文件列表为空。\n";
return array();
}
if(!self::makedirectory() || !@is_writable($this->savepath)) {
$this->log .= $this->savepath . "不能创建或其权限为不可写。\n";
return array();
}
$filearray = array();
foreach($this->filelist as $k=>$f) {
if($this->maxsize && $f['size']>$this->maxsize) {
$this->log .= $f['name'] . "其大小超过了设定的值:" . $this->maxsize ."\n";
}elseif($this->overwrite == false && file_exists($this->savepath . $f['name'])) {
$this->log .= $f['name'] . "已经存在于目录:" . $this->savepath . "\n";
}else{
@unlink($this->savepath . $f['name']);
if(@move_uploaded_file($f['tmp_name'],$this->savepath . mb_convert_encoding($f['name'],'gbk','utf-8'))) {//如果不进行编码转换,中文将无法支持
$this->log .= $f['name'] . "成功上传到目录:". $this->savepath ."\n";
$filearray[$k] = $this->savepath . $f['name'];
}else{
$this->log .= $f['name'] . "上传失败。\n";
}
}
}
return $filearray;
}
/*检测文件的类型
*(array)$files 文件数组
*/
private function check_file_type($files) {
$this->filelist = array();
foreach($files as $key=>$file) {
if($file['error'] == 0) {
$ext = strtolower(substr($file['name'], strrpos($file['name'], '.') + 1));
$str = @file_get_contents($file['tmp_name'],false,null,0,20);
if((in_array($ext,array('jpg','jpeg')) && substr($str ,0, 3) !== "\xff\xd8\xff") || ($ext == 'gif' && substr($str ,0, 4) !== 'gif8') || ($ext == 'png' && substr($str ,0, 8) !== "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a") || ($ext == 'bmp' && substr($str ,0, 2) !== 'bm') || ($ext == 'swf' && (substr($str ,0, 3) !== 'cws' || substr($str ,0, 3) !== 'fws')) || ($ext == 'zip' && substr($str ,0, 4) !== "pk\x03\x04") || ($ext == 'rar' && substr($str ,0, 4) !== 'rar!') || ($ext == 'pdf' && substr($str ,0, 4) !== "\x25pdf") || ($ext == 'chm' && substr($str ,0, 4) !== 'itsf') || ($ext == 'rm' && substr($str ,0, 4) !== "\x2ermf") || ($ext == 'exe' && substr($str ,0, 2) !== "mz") || (in_array($ext,array('doc','xls','ppt')) && substr($str ,0, 4) !== "\xd0\xcf\x11\xe0")) {
$this->log .= $file['name'] . "文件类型与文件内容不符合。\n";
}elseif((!empty($this->extensionfileformat) && in_array($ext,$this->extensionfileformat)) || (empty($this->extensionfileformat) && (in_array($ext,$this->imagefileformat) || in_array($ext,$this->otherfileformat)))) {
$this->filelist[$key] = $file;
}else{
$this->log .= $file['name'] . "不符合上传文件的类型。\n";
@unlink($file['tmp_name']);
}
}
}
}
/*生成上传目录
*
*/
private function makedirectory() {
$directoryname = str_replace("\\","/", $this->savepath);
$dirnames = explode('/', $directoryname);
$total = count($dirnames);
$temp = '';
for($i=0; $i<$total; $i++)
{
$temp .= $dirnames[$i].'/';
if (!is_dir($temp))
{
$oldmask = @umask(0);
if (!@mkdir($temp, 0777)) return false;
@umask($oldmask);
}
};
if(is_dir($this->savepath)) {
return true;
} else {
return false;
};
}
}
?>
上一篇: 奇葩的零售业现象:跑的越快,跌的越狠