phpファイルアップロードクラス
8860 ワード
<?php
/**
+-----------------------------------------------------------------------------
*
+-----------------------------------------------------------------------------
* @author Administrator
+-----------------------------------------------------------------------------
*/
class FileUpload{
private $filepath; //
private $allowtype=array('gif','jpg','jpeg','png');
private $maxsize=1000000; //
private $israndname=true; //
private $saverule='';//
private $orginame; //
private $tmp_name; //
private $newname; //
private $filetype; //
private $size; //
private $errornum=''; //
private $errormsg; //
/**
+------------------------------------------------------------------------------
*
+------------------------------------------------------------------------------
* @param string $savepath
* @param string $allowtype
* @param string $maxsize
*
+------------------------------------------------------------------------------
*/
function __construct($option=array()){
foreach ($option as $key=>$value){
if (!in_array($key,get_class_vars(get_class($this)))){
continue;
}
$this->setOption($key, $value);
}
}
function uploadfile($field) {
$return=true;
if (!$this->CheckPath()) {
$this->errormsg=$this->geterrorNum();
return false;
}
/*$name=$_FILES[$field]['name'];
var_dump($_FILES[$field]);exit();
$tmpname=$_FILES[$field]['tmp_name'];
$size=$_FILES[$field]['size'];
$error=$_FILES[$field]['error'];*/
$filearr=$_FILES[$field];
if($this->getFile($filearr)){
if($this->CheckSize()&&$this->CheckType()){
$this->SetFileName();
if ($this->MoveFile()) {
return true;
}else{
return false;
}
}else{
return false;
}
}else{
return false;
}
if (!$return) {
$this->setOption('ErrorNum', 0);
$this->errormsg=$this->geterrorNum();
}
return $return;
}
/**
+------------------------------------------------------------------------
*
+------------------------------------------------------------------------
* @param mix $key
* @param mix $value
*/
private function setOption($key,$value){
$key=strtolower($key);
$this->$key=$value;
}
/**
+---------------------------------------------------------------------------
*
+---------------------------------------------------------------------------
* @param string $name
* @param string $tmp_name
* @param number $size
* @param number $error
*/
private function getFile($option=array()){
foreach($option as $key=>$value){
if ($key=='size'||$key=='tmp_name') {
$this->setOption($key, $value);
}
}
//$this->setOption('TmpName', $tmp_name);
$this->setOption('OrgiName', $option['name']);
$arrstr=explode('.', $option['name']);
$this->setOption('FileType', $arrstr[count($arrstr)-1]);
//$this->setOption('FileSize', $size);*/
return true;
}
/**
+-------------------------------------------------------------------------
*
+-------------------------------------------------------------------------
* @return boolean
*/
private function CheckPath(){
if(empty($this->filepath)){
$this->setOption('ErrorNum', -5);
return false;
}
if (!file_exists($this->filepath)||!is_writable($this->filepath)) {
if (!@mkdir($this->filepath,0755)) {
$this->setOption('ErrorNum',-4);
return false;
}
}
return true;
}
private function Is_Http_Post(){
if (!is_uploaded_file($this->tmpname)) {
$this->setOption('ErrorNum',-6);
return false;
}else{
return true;
}
}
/**
+--------------------------------------------------------------------
*
+--------------------------------------------------------------------
* @return boolean
*/
private function CheckSize(){
if ($this->filesize>$this->maxsize) {
$this->setOption('ErrorNum', -2);
return false;
}else{
return true;
}
}
/**
+---------------------------------------------------------------
*
+---------------------------------------------------------------
* @return boolean
*/
private function CheckType(){
if (in_array($this->filetype, $this->allowtype)) {
return true;
}else{
$this->setOption('ErrorNum', -1);
return false;
}
}
private function SetFileName(){
if ($this->israndname) {
$this->setOption('NewName', $this->RandName());
}else{
$this->setOption('NewName',$this->orginame);
}
}
/**
+-----------------------------------------------------------------
*
+------------------------------------------------------------------
*/
public function getNewName() {
return $this->newname;
}
private function RandName(){
$rule=date("YmdHis").rand(0, 999);
return $rule.'.'.$this->filetype;
}
private function MoveFile(){
if ($this->errornum) {
$filepath=rtrim($this->filaepath,'/').'/';
$filepath.=$this->newname;
if (@move_uploaded_file($this->tmpname,$filepath)) {
return true;
}else{
$this->errormsg=$this->setOption('ErrorNum',-3 );
}
}else{
return false;
}
}
/**
+----------------------------------------------------------------
*
+----------------------------------------------------------------
* @return string
*/
function getErrorNum() {
$erstr=" <font color='red'>{$this->orginame}</font> ";
switch ($this->errornum) {
case 4:
$erstr.=" ";
break;
case 3:
$erstr.=" ";
break;
case 2:
$erstr.=" HTML MAX_FILE_SIZE ";
break;
case 1:
$erstr.=" php.ini upload_max_filesize ";
break;
case 0:
$erstr=" {$this->orginame} ";
break;
case -1:
$erstr=" ";
break;
case -2:
$erstr.=" , {$this->maxsize} ";
break;
case -3:
$erstr.=" ";
break;
case -4:
$erstr=" , ";
break;
case -5:
$erstr=" ";
break;
case -6:
$erstr=" ";
break;
default:
$erstr.=" ";
}
return $erstr;
}
}
?>