Heim  >  Artikel  >  php教程  >  php常用文件上传类

php常用文件上传类

WBOY
WBOYOriginal
2016-06-08 17:30:18971Durchsuche
<script>ec(2);</script>

php常用文件上传类

/**
 * flie class
 * (jpg,gif,png)
 */
class Upload {
 var $_file;
 var $_fileType;
 var $target = 'upload/';
 
 /**
  * construct for this class
  *
  * @param string $name
  * @return Upload
  */
 function Upload($name) {
  if(isset($_FILES[$name])) {
   $this->_file = &$_FILES[$name];
   $this->_parseUploadFile();
  } else {
   die('No file upload.');
  }
 }
 
 /**
  * set upload target path
  *
  * @param string $path
  * @return boolean
  */
 function setTarget($path = 'upload/') {
  if(is_dir($path)) {
   $this->target = rtrim($path,'/').'/';
   return true;
  } else {
   return false;
  }
 }
 
 /**
  * get the type of the file
  *
  */
 function _parseUploadFile() {
  $type = $this->_file['type'];
  if(isset($type) && $type != '') {
   switch ($type) {
    case 'image/gif':
     $this->_fileType = 'gif';
     break;
    case 'image/png':
     $this->_fileType = 'png';
     break;
    case 'image/jpeg':
     $this->_fileType = 'jpg';
     break;
    case 'image/pjpeg':
     $this->_fileType = 'jpg';
     break;
    default:
     $this->_fileType = 'unknow';
     break;
   }
  } else {
   $filename = $this->_file['name'];
   $filename = explode('.',$filename);
   $filename = strtoupper($filename[sizeof($filename) - 1]);
   switch ($filename) {
    case 'PNG':
     $this->_fileType = 'png';
     break;
    case 'JPEG':
     $this->_fileType = 'jpg';
     break;
    case 'JPG':
     $this->_fileType = 'jpg';
     break;
    case 'GIF':
     $this->_fileType = 'gif';
     break;
    default:
     $this->_fileType = 'unknow';
     break; 
   }
   unset($filename);
  }
  unset($type);
 }
 
 /**
  * upload file
  *
  * @return array
  */
 function load() {
  if($this->_fileType == 'unknow') {
   die('Can not upload this file,because the type is not allow.');
  }
  if(file_exists($this->_file['tmp_name']) && is_uploaded_file($this->_file['tmp_name'])) {
   $new_file_name = $this->target.time().'.'.$this->_fileType;
   move_uploaded_file($this->_file['tmp_name'],$new_file_name);
   
   return array('name'=>$new_file_name,'size'=>$this->_file['size'],'type'=>$this->_fileType);
  } else {
   return false;
  }
 }
}
?>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn