php图片上传类

<?php
class upload
{protected $maxSize=2097152;//允许上传的最大值
 protected $allowExt;//上传文件扩展名
 protected $allowMime;//允许的mime类型
 protected $uploadPath;//上传路径 
 protected $imgFlag;//检测上传文件是否为真正的图片
 protected $file;//通过post获得$_FILES
 protected $error;//获得错号
 protected $ext;//文件扩展名
 protected $destination;//上传路径
 protected $uniName;//文件名
 
 public function __construct($fileName='myFile',$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->file=$_FILES[$this->fileName];
 }
 protected function checkError(){
 	if(!is_null($this->file)){
 		if($this->file['error']>0){
 			switch($this->file['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;
 	}
 }
 protected function checkSize(){    //验证上传文件大小
        if($this->file['size']>$this->maxSize)
        {$this->error="上传文件过大";
        	 return false;}
        else 
        	return true;
  }

protected function checkExt(){//验证文件扩展名
		$this->ext=strtolower(pathinfo($this->file['name'],PATHINFO_EXTENSION));
		if(!in_array($this->ext,$this->allowExt)){
			$this->error='不允许的扩展名';
			return false;
		}
		return true;
	}
  
 protected function checkMime(){//验证文件mime
  	if(!in_array($this->file['type'],$this->allowMime)){
  		$this->error="类型不允许".$this->file['type'];
  		return false;
  	}else 
  		return true;
  }

 protected function checkTrueImg()//验证图片是否为真正的图片还是文本文件修改扩张名后的文件
  {if($this->imgFlag){
  	if(!@getimagesize($this->file['tmp_name'])){
  		$this->error="这不是真正的图片";
  		return false;
  	}else 
  		return true;
  }
  	}
  
 protected function checkHttpPost(){//验证是否是通过http post方式上传的
   if(!is_uploaded_file($this->file['tmp_name'])){
       $this->error="文件不是通过http post方式传上来的";
       return false;		
  	}else
  		return true;
  }
 protected function getUniName(){//获得唯一的文件名
  	return md5(uniqid(microtime(true),true));
  }	
  
  
 protected function checkUploadPath()//验证上传到的文件夹是否存在,不存在就创建一个
  {if (!file_exists($this->uploadPath)){
  	mkdir($this->uploadPath,0777,true);
  }
  }
  
  
 protected function showError(){//显示错误
 	exit('<span style="color:red">'.$this->error.'</span>');
 }
 
 public function uploadFile(){
 	if($this->checkError()&&$this->checkSize()&&$this->checkExt()&&$this->checkMime()&&$this->checkTrueImg()&&$this->checkHTTPPost()){
 		$this->checkUploadPath();
 		$this->uniName=$this->getUniName();
 		$this->destination=$this->uploadPath.'/'.$this->uniName.'.'.$this->ext;
 		if(@move_uploaded_file($this->file['tmp_name'], $this->destination)){
 			return  $this->destination;
 		}else{
 			$this->error='文件移动失败';
 			$this->showError();
 		}
 	}else{
 		$this->showError();
 	}
 }
 
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文件上传是网站开发中常用的功能之一,PHP文件上传可以帮助我们实现文件上传功能。当用户需要上传文件时,可以使用PHP文件上传来处理文件上传的逻辑。PHP文件上传可以实现对上传文件的各种验证,例如文件型、大小、保存路径等。通过使用PHP文件上传,可以让文件上传变得更加安全、简单和高效。 PHP文件上传可以通过封装上传文件的函数来实现文件的上传和下载。上传文件的功能通过对文件的验证和处理,将文件保存到服务器指定的位置。而下载文件的功能通过设置文件的下载头信息,实现对指定文件的下载操作。PHP文件上传还可以处理文件重名、文件大小限制、文件型限制等问题,保证文件上传过程中的安全性和完整性。 在使用PHP文件上传的过程中,需要注意对上传文件的安全性进行严格的检查,避免出现恶意文件上传或者文件被非法下载的情况。同时,需要确保服务器环境对文件上传和下载的操作进行了正确的配置,以确保文件上传和下载功能的正常运行。 总的来说,PHP文件上传可以极大地方便我们在网站开发中对文件的上传和下载操作,帮助我们完成文件处理的各种功能。无论是图片、文档还是音视频文件,PHP文件上传都可以帮助我们完成文件上传和下载功能,为网站的用户提供更好的使用体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值