首页 > php教程 > PHP源码 > 正文

php常用文件上传类

php中文网
发布: 2016-06-08 17:30:18
原创
1164人浏览过
<script>ec(2);</script>

php常用文件上传类

PHP多文件上传插件
PHP多文件上传插件

PHP多文件上传插件

PHP多文件上传插件 149
查看详情 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;
  }
 }
}
?>

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号