マルチファイルアップロードクラス
3558 ワード
<?php
/*
PHP
:YY 88354001
:www.zixue.it
: $_FILES['name'].$_FIELS['error'].. ,
1:
2:
3:
4:
5:
0, , 。
*/
defined('ACC')||exit('NO PERMISSION');
class Upload{
//allow
protected $filesExt = "jpg,png,gif,bmp,jpeg,rar,exe";
//
public $upErr = ' !';
//
public $filesPath = array();
// 1M;
protected $maxSize = 1;
// $_FILES temp
protected $files = null;
//
protected $error = array(
0=>" ",
1=>' ',
2=>' HTML POST ',
3=>' ',
4=>' ',
6=>' ',
7=>' ',
8=>' ',
9=>' ',
10=>' ',
11=>' ',
12=>' '
);
//
public function Up($files){
$rs = array_values($files);
$files = $rs[0];
if(is_array($files['name'])){
$this->files = $files;
}else{
foreach($files as $key => $value){
$res[$key][0] = $value;
}
$this->files = $res;
}
for($i=0;$i<count($this->files['name']);$i++){
if($this->files['error'][$i]!=0){
$this->upErr = $this->error[$this->files['error'][$i]];
return false;
}
if(!$this->IsExt($i)){
$this->upErr = $this->error[8];
return false;
}
if(!$this->Mk_dir()){
$this->upErr = $this->error[10];
return false;
}
if(!$this->IsMax($i)){
$this->upErr = $this->error[9];
return false;
}
if(!$this->FileRename()){
$this->upErr = $this->error[12];
return false;
}
if(!$this->MoveFiles($i)){
$this->upErr = $this->error[11];
return false;
}
}
if(count($this->filesPath)==1){
return $this->filesPath[0];
}else{
return $this->filesPath;
}
}
//
protected function GetExt($i=0){
return end(explode('.',$this->files['name'][$i]));;
}
//
protected function IsExt(){
if(in_array($this->GetExt(),explode(',',$this->filesExt))){
return true;
}else{
return false;
}
}
//
protected function IsMax($i){
if($this->files['size'][$i] < $this->maxSize*(1024*1024)){
return true;
}else{
return false;
}
}
//
protected function Mk_dir(){
$dir = ROOT.'data/images/'.date('ym/d');
if(is_dir($dir)||mkdir($dir,0777,true)){
return true;
}else{
return false;
}
}
//
protected function FileRename(){
$str = "abcdefghijklmnopqrstuvwxyz1234567890";
return date('ymd').substr(str_shuffle($str),0,6).'.'.$this->GetExt();
}
//
protected function MoveFiles($i){
$filesPath = ROOT."data/images/".date('ym/d').'/'.$this->FileRename();
if(move_uploaded_file($this->files['tmp_name'][$i],$filesPath)){
$filesPath = str_replace(ROOT,'',$filesPath);
$this->filesPath[] = $filesPath;
return true;
}else{
return false;
}
}
}