<?php
/**
* param string $fileName
* param string $maxSize
* param string uploadPath
* param array $allowMime
* param array $endExt
* param array $fileInfo
*/
//面向对象写一个文件上传
class upload{
protected $fileName;
protected $maxSize;
protected $endExt;
protected $uploadPath;
protected $flage;
protected $allowMime;
protected $fileInfo;
protected $error;
protected $ext;
protected $destination;
protected $uniqid;
//构造函数,自己上传的数据
public function __construct($fileName='myfile',
$maxSize=5242880,//5M
$endExt=array('jpg','jpeg','png','gif'),
$uploadPath='./uploads',
$flage=true,
$allowMime=array('image/jpg','image/jpeg','image/png','image/gif')
){
$this->fileName=$fileName;//浏览框名称
$this->maxSize=$maxSize;//服务器端大小限制
$this->endExt=$endExt;//限制用户只能在自己设置的后缀名称内上传指定类型文件
$this->uploadPath=$uploadPath;//上传到指定m目录
$this->flage=$flage;//是否检测文件类型
$this->allowMime=$allowMime;//通过文件类型来判断该文件是否是恶意修改后缀来获得上传
$this->fileInfo=$_FILES[$this->fileName];//上传文件的信息保存在变量fileInfo中
/*接收到上传的信息$_FILES数组中自己要接受的fileName的信息*/
}
//判断错误号,由于在对象内使用,设置为私有
protected function checkERROR(){
if ($this->fileInfo['error']>0) {
switch ($fileInfo['error']) {
case 1:
$this->error='有错误发生! UPLOAD_ERR_INI_SIZE :其值为1,上传的文件超过了php.ini中的upload_max_filesize选项中限制的值';
break;
case 2:
$this->error='有错误发生! UPLOAD_ERR_FORM_SIZE
:其值为2,上传的文件大小超过了HTML表单中max_file_size限制的值';
break;
case 3:
$this->error='有错误发生! UPLOAD_ERR_PARTIAL:其值为3,部分文件未上传';
break;
case 4:
$this->error='有错误发生! UPLOAD_ERR_NO_FILE:其值为4,没有文件被上传';
break;
case 6:
$this->error='有错误发生! UPLOAD_ERR_NO_TMP_DIR:其值为6,找不到临时文件夹';
break;
case 7:
$this->error='有错误发生! UPLOAD_ERR_CANT_WRITE:其值为7,文件写入失败';
break;
case 8:
$this->error='有错误发生! UPLOAD_ERR_EXTENSION:其值为8,上传的文件被PHP扩展程序中断';
break;
}
return flase;
}
return true;
}
// 判断文件大小是否符合规定
protected function checkMax(){
if ($this->maxSize<$this->fileInfo['size']) {
$this->error='上传文件过大';
return flase;
}
return true;
}
//检测扩展名是否在允许的范围内
protected function checkExt(){
//用到了$ext,就设置一个$ext属性
$this->ext=strtolower(pathinfo($this->fileinfo['name'],PATHINFO_EXTENSION));
if (!is_array($this->ext,$this->endExt)) {
$this->error='扩展名不符合要求';
return flase;
}
return true;
}
//检测上传方式是否为post
protected function checkHttpPost(){
if (!is_uploaded_file($this->fileInfo['tmp_name'])) {
$this->error='上传方式不符合要求';
return flase;
}
return true;
}
//检测文件类型
protected function checkMime(){
if (!is_array($this->allowMime,$this->endExt){
$this->error='文件类型MIME非法';
return flase;
}
return true;
}
//检测是否为一个真实的图片
protected function checekimage(){
if ($this->flage) {
if (!getimagesize($this->fileInfo['tmp_name'])) {
$this->error='文件不是一个真实的图片';
return flase;
}
return ture;
}
}
//显示错误
protected function ShowError(){
exit('<span style:"color=red"><strong>'.$this->error.'</strong></span>');
}
//判断是否目录名是否存在
protected function File(){
if (!file_exists($this->uploadPath)) {
mkdir($uploadPath,0777,true);
chmod($uploadPath,0777);
}
}
//上传文件
public function uploadfile(){
if ($this->checkMax()&&$this->checkHttpPost()&&$this->checkMime()&&$this->checkExt()&&$this->checkERROR()&&checekimage()){
$this->uniqid=$this->setuniqid();
$this->destination=$this->uploadPath.'/'.$this->uniqid.'.'.$this->ext;
//移动文件
if (@move_uploaded_file($this->fileInfo['tmp_name'],$this->destination)) {
return $this->destination;
}else{
$this->error='文件移动失败!';
$this->ShowError();
}
}else{
$this->ShowError();
}
}
}
?>
顶不住了 你这个BUG比我的还多 我还想复制粘贴省事然后整到一半重新自己写了
is_array改成in_array试试行不行
第107行上下的代码在这里...