php之上传类
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2016/5/26
* Time: 20:29
*/ class upload{
protected $fileName;
protected $maxSize;
protected $allowMime;
protected $allowExt;
protected $uploadPath;
protected $imgFlag;
protected $fileInfo;
protected $error;
protected $ext;
/**
* @param string $fileName
* @param string $uploadPath
* @param string $imgFlag
* @param number $maxSize
* @param array $allowExt
* @param array $allowMime
*/
public function __construct($fileName='photo',$uploadPath='./uploads',$imgFlag=true,$maxSize=5242880,$allowExt=array('jpeg','jpg','png','gif'),$allowMime=array('image/jpeg','image/png','image/gif')){
$this->fileName=$fileName;
$this->maxSize=$maxSize;
$this->allowMime=$allowMime;
$this->allowExt=$allowExt;
$this->uploadPath=$uploadPath;
$this->imgFlag=$imgFlag;
$this->fileInfo=$_FILES[$this->fileName];
}
/**
* 检测上传文件是否出错
* @return boolean
*/
protected function checkError(){
if(!is_null($this->fileInfo)){
if($this->fileInfo['error']>0){
switch($this->fileInfo['error']){
case 1:
$this->error='超过了PHP配置文件中upload_max_filesize选项的值';
break;
case 2:
$this->error='超过了表单中MAX_FILE_SIZE设置的值';
break;
case 3:
$this->error='文件部分被上传';
break;
case 4:
$this->error='没有选择上传文件';
break;
case 6:
$this->error='没有找到临时目录';
break;
case 7:
$this->error='文件不可写';
break;
case 8:
$this->error='由于PHP的扩展程序中断文件上传';
break; }
return false;
}else{
return true;
}
}else{
$this->error='文件上传出错';
return false;
}
}
/**
* 检测上传文件的大小
* @return boolean
*/
protected function checkSize(){
if($this->fileInfo['size']>$this->maxSize){
$this->error='上传文件过大';
return false;
}
return true;
}
/**
* 检测扩展名
* @return boolean
*/
protected function checkExt(){
$this->ext=strtolower(pathinfo($this->fileInfo['name'],PATHINFO_EXTENSION));
if(!in_array($this->ext,$this->allowExt)){
$this->error='不允许的扩展名';
return false;
}
return true;
}
/**
* 检测文件的类型
* @return boolean
*/
protected function checkMime(){
if(!in_array($this->fileInfo['type'],$this->allowMime)){
$this->error='不允许的文件类型';
return false;
}
return true;
}
/**
* 检测是否是真实图片
* @return boolean
*/
protected function checkTrueImg(){
if($this->imgFlag){
if(!@getimagesize($this->fileInfo['tmp_name'])){
$this->error='不是真实图片';
return false;
}
return true;
}
}
/**
* 检测是否通过HTTP POST方式上传上来的
* @return boolean
*/
protected function checkHTTPPost(){
if(!is_uploaded_file($this->fileInfo['tmp_name'])){
$this->error='文件不是通过HTTP POST方式上传上来的';
return false;
}
return true;
}
/**
*显示错误
*/
protected function showError(){
exit('<span style="color:red">'.$this->error.'</span>');
}
/**
* 检测目录不存在则创建
*/
protected function checkUploadPath(){
if(!file_exists($this->uploadPath)){
mkdir($this->uploadPath,0777,true);
}
}
/**
* 产生唯一字符串
* @return string
*/
protected function getUniName(){
return md5(uniqid(microtime(true),true));
}
/**
* 上传文件
* @return string
*/
public function uploadFile(){
if($this->checkError()&&$this->checkSize()&&$this->checkExt()&&$this->checkMime()&&$this->checkTrueImg()&&$this->checkHTTPPost()){
$this->checkUploadPath();
$this->names=$this->getUniName();
$this->destination=$this->uploadPath.'/'.$this->names.'.'.$this->ext;
if(@move_uploaded_file($this->fileInfo['tmp_name'], $this->destination)){
return $this->destination;
}else{
$this->error='文件移动失败'; }
}else{
$this->showError();
}
}
}
php之上传类的更多相关文章
- ThinkPHP---TP功能类之上传
		
[一]概论 (1)上传操作的核心操作:移动临时文件(move_upload_file),在ThinkPHP里封装了上传类Upload.class.php (2)上传类Upload.class.php代 ...
 - THINKPHP源码学习--------文件上传类
		
TP图片上传类的理解 在做自己项目上传图片的时候一直都有用到TP的上传图片类,所以要进入源码探索一下. 文件目录:./THinkPHP/Library/Think/Upload.class.php n ...
 - PHP图片上传类
		
前言 在php开发中,必不可少要用到文件上传,整理封装了一个图片上传的类也很有必要. 图片上传的流程图 一.控制器调用 public function upload_file() { if (IS_P ...
 - Ueditor 1.4.3.1 使用 ThinkPHP 3.2.3 的上传类进行图片上传
		
在 ThinkPHP 3.2.3 中集成百度编辑器最新版 Ueditor 1.4.3.1,同时将编辑器自带的上传类替换成 ThinkPHP 3.2.3 中的上传类. ① 下载编辑器(下载地址:http ...
 - webpy分页类 + 上传类
		
webpy没有分页类.按照php的思路.自己编了一个.数据库用的是sqlite. class Page(object): '''分页类''' def __init__(self,page_size,d ...
 - ASP.NET 文件上传类 简单好用
		
调用: UploadFile uf = new UploadFile(); /*可选参数*/ uf.SetIsUseOldFileName(true);//是否使用原始文件名作为新文件的文件名(默认: ...
 - PHP多文件上传类
		
<?php class Upload{ var $saveName;// 保存名 var $savePath;// 保存路径 var $fileFormat = array('gif','jpg ...
 - PHP 文件上传类
		
FileUpload.; $]; $_newname = date(,). : To ...
 - php四个常用类封装 :MySQL类、 分页类、缩略图类、上传类;;分页例子;
		
Mysql类 <?php /** * Mysql类 */ class Mysql{ private static $link = null;//数据库连接 /** * 私有的构造方法 */ pr ...
 
随机推荐
- UILabel详解
			
// ----------------------UILabel--------------------------- UILabel *label = [[UILabel alloc] initWi ...
 - Java基础之读文件——使用通道随机读写文件(RandomReadWrite)
			
控制台程序,使用通道随机读写primes_backup.bin文件. import static java.nio.file.StandardOpenOption.*; import java.nio ...
 - nginx:配置详细说明
			
一.fastcgi param 详情: fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#脚本文件请求的路径 fast ...
 - java jps命令
			
jps是jdk提供的一个查看当前java进程的小工具, 可以看做是JavaVirtual Machine Process Status Tool的缩写.非常简单实用. 命令格式:jps [option ...
 - iOS 工程中文件变成红色是什么情况
			
iOS 工程中文件变成红色是原有的文件路径改变了,系统找不到了.
 - FB面经prepare: Task Schedule
			
每种task都有冷却时间,比如task1执行后,要经过interval时间后才能再次执行,求总共所需时间. 用HashMap保存每一个task的下一次可以开始执行的最早时间 package TaskS ...
 - 【Origin】 叹文
			
行文如流水, 千字挥手就: 偏偏伤脑筋, 哪得轻松事. -作于二零一五年五月三十日
 - centos python nginx uwsgi
			
先更新系统,并安装编译环境等等. yum update yum install python python-devel libxml2 libxml2-devel python-setuptools ...
 - PHP浮点数的一个常见问题的解答 (转载 http://www.laruence.com/2013/03/26/2884.html)
			
不过, 我当时遗漏了一点, 也就是对于如下的这个常见问题的回答: <?php $f = 0.58; var_dump(intval($f * 100)); //为啥输出57 ?> 为啥输出 ...
 - 解决ScrollView与ListView事件冲突
			
1,在最近做项目的时候使用ScrollView嵌套ListView的时候发现ListView的滑动效果失效,简单的网上搜索了一下,也就有了下面的解决方法,在ListView中设置事件的监听listvi ...