php实现文件上传的类
php实现文件上传的类 **upload.class.php**```{.php}lt;?phpclass Upload{ private $allowTypes = array(#39;gif#39;,#39;jpg#39;,#39;png#39;,#39;bmp#39;); private $uploadPath = null; private $maxSize = 1000000;
upload.class.php
<?php
class Upload
{
private $allowTypes = array('gif','jpg','png','bmp');
private $uploadPath = null;
private $maxSize = 1000000;
private $msgCode = null;
public function __construct($options = array())
{
//取类内的所有变量
$vars = get_class_vars(get_class($this));
//设置类内变量
foreach ($options as $key=>$value) {
if (array_key_exists($key, $vars)) {
$this->$key = $value;
}
}
}
public function fileUpload($myfile)
{
$name = $myfile['name'];
$tmpName = $myfile['tmp_name'];
$error = $myfile['error'];
$size = $myfile['size'];
//检查上传文件的大小 or 类型 and 上传的目录
if ($error > 0) {
$this->msgCode = $error;
return false;
} elseif (!$this->checkType($name)) {
return false;
} elseif (!$this->checkSize($size)) {
return false;
} elseif (!$this->checkUploadFolder()) {
return false;
}
$newFile = $this->uploadPath . '/' . $this->randFileName($name);
//复制文件到上传目录
if (!is_uploaded_file($tmpName)) {
$this->msgCode = -3;
return false;
} elseif(@move_uploaded_file($tmpName, $newFile)) {
$this->msgCode = 0;
return true;
} else {
$this->msgCode = -3;
return false;
}
}
/**
* 检查上传文件的大小有没有超过限制
*
* @var int $size
* @return boolean
*/
private function checkSize($size)
{
if ($size > $this->maxSize) {
$this->msgCode = -2;
return false;
} else {
return true;
}
}
/**
* 检查上传文件的类型
*
* @var string $fileName
* @return boolean
*/
private function checkType($fileName)
{
$arr = explode(".", $fileName);
$type = end($arr);
if (in_array(strtolower($type), $this->allowTypes)) {
return true;
} else {
$this->msgCode = -1;
return false;
}
}
/**
* 检查上传的目录是否存在,如不存在尝试创建
*
* @return boolean
*/
private function checkUploadFolder()
{
if (null === $this->uploadPath) {
$this->msgCode = -5;
return false;
}
$this->uploadPath = rtrim($this->uploadPath,'/');
$this->uploadPath = rtrim($this->uploadPath,'\\');
if (!file_exists($this->uploadPath)) {
if (@mkdir($this->uploadPath, 0755)) {
return true;
} else {
$this->msgCode = -4;
return false;
}
} elseif(!is_writable($this->uploadPath)) {
$this->msgCode = -3;
return false;
} else {
return true;
}
}
/**
* 生成随机文件名
*
* @var string $fileName
* @return string
*/
private function randFileName($fileName)
{
$salt = 'ilovechina';
list($name,$type) = explode(".",$fileName);
$newFile = crypt(md5($name),$salt);
return $newFile . '.' . $type;
}
/*
* 取上传的结果和信息
*
* @return array
*/
public function getStatus()
{
$messages = array(
4 => "没有文件被上传",
3 => "文件只被部分上传",
2 => "上传文件超过了HTML表单中MAX_FILE_SIZE选项指定的值",
1 => "上传文件超过了php.ini 中upload_max_filesize选项的值",
0 => "上传成功",
-1 => "末充许的类型",
-2 => "文件过大,上传文件不能超过{$this->maxSize}个字节",
-3 => "上传失败",
-4 => "建立存放上传文件目录失败,请重新指定上传目录",
-5 => "必须指定上传文件的路径"
);
return array('error'=>$this->msgCode, 'message'=>$messages[$this->msgCode]);
}
}
demo.php
<?php
require_once 'upload.class.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['sub'])) {
$upload = new Upload(array('uploadPath'=>'./upfiles'));
$upload->fileUpload($_FILES['myfile']);
print_r($upload->getStatus());
}
?>
<html>
<head>
<title>无刷新文件上传</title>
<meta content-type="text/html" charset="utf-8" />
</head>
<body style="text-align:center">
<div name="upload-status"></div>
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<input type="file" name="myfile" />
<input type="submit" name="sub" value="upload"/>
</form>
</body>
</html>
精彩图集
精彩文章






