gusucode.com > 同城苏州黄页系统php源码程序 > lib/upload.class.php
<?php /* 上傳文件類,支持多文件 使用方法: $up = new UploadFile(存儲路徑,最大上傳值 ) $up->SetAllowType(array('.gif','.jpg','.'jpeg','.pne'));//設置允許上傳文件類型 $up->SetMaxSize(1024);//設置允許上傳文件最大值,默認2M $up->upload(上傳文件表單名字);//表單爲空返回假值,否則無返回值 返回值(以下值皆爲數組形式): $up->success;//正確上傳文件 $up->error;//錯誤上傳的文件 $up->except;//不允許上傳的文件 注: 如果有兩個不同名字的文件上傳表單, 在調用$up->upload(第二個表單名字);之前請先用$up->reset()方法清空第一個表單上傳的返回值 */ class UploadFile { var $storePath; // 上傳的東西的存儲路徑 var $maxSize; // 允許上傳的最大尺寸 var $allowType; // 允許上傳的類型 var $except;//返回值類型,不允許上傳的文件,結果如:$arr[$key]=$filename var $error;//返回值類型,上傳錯誤的文件,結果如:$arr[]=array('name'=>$fileName,'msg'=>$errorMsg) var $success;//返回值類型,上傳正確的文件,結果如:$arr[]=array('oldName'=$oldName,'newName'=>$newName) //構造函數 function UploadFile($storePath, $maxSize=1048576) { $this->SetStorePath($storePath); $this->SetMaxSize($maxSize); $this->SetAllowType(array('.txt','.jpg','.jpeg','.bmp','.gif','.png','.rar','.zip')); $this->except = array(); $this->error = array(); $this->success= array(); }//------ /* 開始上傳 $formName 表單名稱 $newName 文件保存名,不填將自動産生 $multi 是否爲多個文件 ? */ function Upload($formName) { //echo "empty(\$_FILES):".empty($_FILES)."<br>"; if (empty($_FILES)) {return false;}// else {extract($_FILES)}; //echo "here"; //如果是單個文件地話,把它轉換成數組,當成多文件處理 is_array($_FILES[$formName]['name'])?$arrName = $_FILES[$formName]['name']:$arrName[]=$_FILES[$formName]['name']; is_array($_FILES[$formName]['type'])?$arrType = $_FILES[$formName]['type']:$arrType[]=$_FILES[$formName]['type']; is_array($_FILES[$formName]['size'])?$arrSize = $_FILES[$formName]['size']:$arrSize[]=$_FILES[$formName]['size']; is_array($_FILES[$formName]['tmp_name'])?$arrTmpName = $_FILES[$formName]['tmp_name']:$arrTmpName[]=$_FILES[$formName]['tmp_name']; is_array($_FILES[$formName]['error'])?$arrError = $_FILES[$formName]['error']:$arrError[]=$_FILES[$formName]['error']; $sort = -1;//第幾圖片 foreach ($arrName as $key=>$fileName) { $sort++; $fileType = $arrType[$key]; $fileSize = $arrSize[$key]; $fileTmpName = $arrTmpName[$key]; $fileError = $arrError[$key]; //$newName = $this->MakeFileName($fileName); if(!empty($fileTmpName))$newName = md5_file($fileTmpName).rand(0,65535).$this->GetFileExt($fileName); //echo "\$fileError:$fileError<br>"; //是否有文件上傳? //echo "$fileName:$fileType:$fileSize:$fileTmpName:$fileError:$newName:".$this->maxSize."<br>"; if (4==$fileError) {continue;}//$sort++;} //判斷大小 if ($fileSize>$this->maxSize || 1==$fileError || 2==$fileError || 3==$fileError) { $this->SetError(array('name'=>$fileName, 'msg'=>'文件大小超過限制')); //$sort++; continue; } //判斷是否是允許的類型 if (!$this->IsAllowType($fileName)) { $this->SetExcept(array('name'=>$fileName, 'msg'=>'不允許上傳的類型')); //$sort++; continue; } //echo "here"; //開始上傳 //opendir($this->storePath); //$mkresult = mkdir($this->storePath, 0777); //var_dump(is_dir($this->storePath));echo "<br>"; //echo $mkresult."<br>"; //echo "\$fileTmpName:$fileTmpName<br>"; //echo "newfielname:".$this->storePath.$newName."<br>"; if(!is_dir($this->storePath)&&!mkdir($this->storePath, 0777)) { $this->SetError(array('name'=>$fileName, 'msg'=>'創建上載目錄失敗')); break; } else if (@move_uploaded_file($fileTmpName, $this->storePath.$newName)) //else if (copy($this->storePath."blank.gif", $this->storePath.$newName)) { //echo $this->storePath.$newName."<br>"; $this->SetSuccess(array('sort'=>$sort, 'oldName'=>$fileName,'newName'=>$newName)); //$sort++; } else { //copy($fileTmpName, $this->storePath.$newName); //echo "is_uploaded_file:".is_uploaded_file($fileTmpName)."<br>";exit; //echo $this->storePath."blank.gif----".$this->storePath.$newName;exit; //echo "err<br>"; $this->SetError(array('name'=>$fileName, 'msg'=>'保存失敗')); //$sort++; } //$sort++; }//foreach }//upload //複位三個返回值 function Reset() { $this->except = array(); $this->error = array(); $this->success= array(); } //設置錯誤信息 function SetError($arr) { $this->error[] = $arr; } //設置不允許上傳文件信息 function SetExcept($arr) { $this->except[] = $arr; } //設置成功上傳文件信息 function SetSuccess($arr) { $this->success[] = $arr; } //判斷是否爲允許的文件 function IsAllowType($fileName) { $ext = strtolower($this->GetFileExt($fileName)); return in_array($ext, $this->allowType); } //設置保存路徑,$path以/結尾 function SetStorePath($path='./') { $this->storePath = $path; } //設置大小 function SetMaxSize($size=1048576) { $this->maxSize = $size; } /* 獲取文件擴展名 .xxx */ function GetFileExt($fileName) { $arr = explode('.', $fileName); if (1==count($arr)) return ''; else return '.'.$arr[count($arr)-1]; } /* 設置允許上傳的文件類型 $allowType 如:array('.gif','jpg'); */ function SetAllowType($allowType) { if (empty($allowType) || !is_array($allowType))//是否爲空? { $this->allowType = array(); } else { $this->allowType = array_change_key_case($allowType, CASE_LOWER); } } //判斷是否爲圖片文件 function IsImage($fileName){ $img = array('.gif','.jpg','.png','.swf','.psd','.bmp'); $ext = strtolower($this->GetFileExt($fileName)); return in_array($ext,$img); } /* 産生文件名 $fileName 是老文件名,要從裏面提取擴展名的 */ function MakeFileName($fileName) { $ext = UploadFile::GetFileExt($fileName); $name = microtime(); $name = str_replace(' ','',$name); $name = str_replace('0.','',$name); return $name.$ext; } }//class`---- ?>