使用方法:
$file = new upload;
$file->uploadFile('face', rand().'/');
第二个参数是用来灵活定义存放目录的(比如可以给每一个用户按照用户UID来存放),当然可默认为空(存放地址就按照配置方法中的地址)
header('Content-Type:text/html; charset=utf-8');
class upload{
private function conf($key){
$conf = array(
'size' => 2000000,
'type' => 'jpg|jpeg|gif|png',
'path' => './images/face/',
'rename' => uniqid().gmdate('YmdHis')
);
return $conf[$key];
}
private function filext($name){
return strrchr($name, '.');
}
private function direname($rename){
$dirname = dirname($rename);
if(is_dir($dirname)){
return $rename;
}else{
@mkdir($dirname, 0755, true);
return $rename;
}
}
public function uploadFile($fileName, $userDiyPath = ''){
$tmp = $_FILES[$fileName];
if(is_uploaded_file($tmp['tmp_name'])){
if(preg_match('/'.$this->conf('type').'/i', $tmp['type'])){
if($tmp['size'] < $this->conf('size')){
if($tmp['error'] == 0){
$rename = $this->direname($this->conf('path').$userDiyPath.$this->conf('rename').$this->filext($tmp['name']));
if(@move_uploaded_file($tmp['tmp_name'], $rename)){
return $rename;
}else{
js::reload('文件上传出现错误!');
}
}else{
js::reload('文件上传出现错误!');
}
}else{
js::reload('文件上传大小错误!');
}
}else{
js::reload('文件上传类型错误!');
}
}else{
js::reload('文件上传方式错误!');
}
}
}
class js{
public static function reload($msg){
exit($msg);
}
}
$file = new upload;
$file->uploadFile('face', rand().'/');