提交单文件的页面
upload.PHP
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>文件上传</title>
- </head>
- <body>
- <form action="doAction.php" method="post" enctype="multipart/form-data">
- <!-- 在客户端可以设置表单MAX_FILE_SIZE限制的大小 -->
- <!--<input type="hidden" name="MAX_FILE_SIZE" value="5*1024*1024">-->
- 请选择您要上传的文件:
- <!-- 在客户端设置允许上传的文件类型 --><!-- 但是我们需要记住:客户端所做的任何限制都是不安全的 -->
- <!--<input type="file" name="myFile" accept="image/jpeg,image/gif,image/png"/><br/>-->
- <input type="file" name="myFile2" /><br/>
- <input type="submit" value="上传文件"/>
- </form>
- </body>
- </html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<form action="doAction.php" method="post" enctype="multipart/form-data">
<!-- 在客户端可以设置表单MAX_FILE_SIZE限制的大小 -->
<!--<input type="hidden" name="MAX_FILE_SIZE" value="5*1024*1024">-->
请选择您要上传的文件:
<!-- 在客户端设置允许上传的文件类型 --><!-- 但是我们需要记住:客户端所做的任何限制都是不安全的 -->
<!--<input type="file" name="myFile" accept="image/jpeg,image/gif,image/png"/><br/>-->
<input type="file" name="myFile2" /><br/>
<input type="submit" value="上传文件"/>
</form>
</body>
</html>
单文件上传类 Upload.class.php
- <?php
- /**
- * Created by PhpStorm.
- * User: DreamBoy
- * Date: 2016/4/9
- * Time: 9:24
- */
- error_reporting(0);
- class Upload {
- protected $fileName; //POST请求时文件的name值
- protected $maxSize; //文件上传的最大大小
- protected $allowMime; //允许上传的文件类型
- protected $allowExt; //允许上传的文件类型
- protected $uploadPath; //文件上传的路径
- protected $imgFlag; //标志是否要求上传的文件为真实图片
- protected $fileInfo; //上传文件的信息
- protected $ext; //文件扩展名
- protected $uniName; //产生的文件唯一名称(不包含后缀)
- protected $destination; //文件上传的目录
- protected $error; //记录错误号
- protected $err = array( //错误号及错误类型
- '001' => '超过了PHP配置文件中upload_max_filesize选项值',
- '002' => '超过了表单中MAX_FILE_SIZE设置的值',
- '003' => '文件部分被上传',
- '004' => '没有选择上传文件',
- '005' => '没有找到临时目录',
- '006' => '文件不可写',
- '007' => '由于PHP的扩展程序中断文件上传',
- '008' => '上传文件过大',
- '009' => '不允许的文件类型',
- '010' => '不允许的文件MIME类型',
- '011' => '文件不是真实图片',
- '012' => '文件不是通过HTTP POST方式上传上来的',
- '013' => '文件移动失败',
- '014' => '系统错误:文件上传出错',
- );
- /**
- * Upload constructor.
- * @param string $fileName
- * @param string $uploadPath
- * @param bool $imgFlag
- * @param int $maxSize
- * @param array $allowExt
- * @param array $allowMime
- */
- 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->fileInfo = $_FILES[$this->fileName];
- $this->ext = strtolower(pathinfo($this->fileInfo['name'], PATHINFO_EXTENSION));
- }
- /**
- * 检测上传文件是否出错
- * @return bool
- */
- protected function checkError() {
- if(is_null($this->fileInfo)) { //文件获取失败
- $this->error = '014';
- return false;
- }
- if($this->fileInfo['error']>0) {
- switch($this->fileInfo['error']) {
- case 1:
- $this->error = '001';
- break;
- case 2:
- $this->error = '002';
- break;
- case 3:
- $this->error = '003';
- break;
- case 4:
- $this->error = '004';
- break;
- case 6:
- $this->error = '005';
- break;
- case 7:
- $this->error = '006';
- break;
- case 8:
- $this->error = '007';
- break;
- }
- return false;
- }
- return true;
- }
- /**
- * 检测上传文件的大小
- * @return bool
- */
- protected function checkSize() {
- if($this->fileInfo['size'] > $this->maxSize) {
- $this->error = '008';
- return false;
- }
- return true;
- }
- /**
- * 检测文件扩展名
- * @return bool
- */
- protected function checkExt() {
- if(!in_array($this->ext, $this->allowExt)) {
- $this->error = '009';
- return false;
- }
- return true;
- }
- /**
- * 检测文件的MIME类型
- * @return bool
- */
- protected function checkMime() {
- if(!in_array($this->fileInfo['type'],$this->allowMime)) {
- $this->error = '010';
- return false;
- }
- return true;
- }
- /**
- * 检测文件是否为真实图片
- * @return bool
- */
- protected function checkTrueImg() {
- if($this->imgFlag) {
- if(!@getimagesize($this->fileInfo['tmp_name'])) {
- $this->error = '011';
- return false;
- }
- }
- return true;
- }
- /**
- * 检测是否通过HTTP Post方式上传过来的
- * @return bool
- */
- protected function checkHTTPPost() {
- if(!is_uploaded_file($this->fileInfo['tmp_name'])) {
- $this->error = '012';
- return false;
- }
- return true;
- }
- /**
- * 显示错误
- */
- protected function showError() {
- $e = $this->err[$this->error];
- exit('<span style="color:red">' . $e . '</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->uniName = $this->getUniName();
- $this->destination = $this->uploadPath . '/' . $this->uniName . '.' . $this->ext;
- if(@move_uploaded_file($this->fileInfo['tmp_name'], $this->destination)) {
- return $this->destination;
- } else {
- $this->error = '013';
- $this->showError();
- }
- } else {
- $this->showError();
- }
- }
- }
<?php
/**
* Created by PhpStorm.
* User: DreamBoy
* Date: 2016/4/9
* Time: 9:24
*/
error_reporting(0);
class Upload {
protected $fileName; //POST请求时文件的name值
protected $maxSize; //文件上传的最大大小
protected $allowMime; //允许上传的文件类型
protected $allowExt; //允许上传的文件类型
protected $uploadPath; //文件上传的路径
protected $imgFlag; //标志是否要求上传的文件为真实图片
protected $fileInfo; //上传文件的信息
protected $ext; //文件扩展名
protected $uniName; //产生的文件唯一名称(不包含后缀)
protected $destination; //文件上传的目录
protected $error; //记录错误号
protected $err = array( //错误号及错误类型
'001' => '超过了PHP配置文件中upload_max_filesize选项值',
'002' => '超过了表单中MAX_FILE_SIZE设置的值',
'003' => '文件部分被上传',
'004' => '没有选择上传文件',
'005' => '没有找到临时目录',
'006' => '文件不可写',
'007' => '由于PHP的扩展程序中断文件上传',
'008' => '上传文件过大',
'009' => '不允许的文件类型',
'010' => '不允许的文件MIME类型',
'011' => '文件不是真实图片',
'012' => '文件不是通过HTTP POST方式上传上来的',
'013' => '文件移动失败',
'014' => '系统错误:文件上传出错',
);
/**
* Upload constructor.
* @param string $fileName
* @param string $uploadPath
* @param bool $imgFlag
* @param int $maxSize
* @param array $allowExt
* @param array $allowMime
*/
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->fileInfo = $_FILES[$this->fileName];
$this->ext = strtolower(pathinfo($this->fileInfo['name'], PATHINFO_EXTENSION));
}
/**
* 检测上传文件是否出错
* @return bool
*/
protected function checkError() {
if(is_null($this->fileInfo)) { //文件获取失败
$this->error = '014';
return false;
}
if($this->fileInfo['error']>0) {
switch($this->fileInfo['error']) {
case 1:
$this->error = '001';
break;
case 2:
$this->error = '002';
break;
case 3:
$this->error = '003';
break;
case 4:
$this->error = '004';
break;
case 6:
$this->error = '005';
break;
case 7:
$this->error = '006';
break;
case 8:
$this->error = '007';
break;
}
return false;
}
return true;
}
/**
* 检测上传文件的大小
* @return bool
*/
protected function checkSize() {
if($this->fileInfo['size'] > $this->maxSize) {
$this->error = '008';
return false;
}
return true;
}
/**
* 检测文件扩展名
* @return bool
*/
protected function checkExt() {
if(!in_array($this->ext, $this->allowExt)) {
$this->error = '009';
return false;
}
return true;
}
/**
* 检测文件的MIME类型
* @return bool
*/
protected function checkMime() {
if(!in_array($this->fileInfo['type'],$this->allowMime)) {
$this->error = '010';
return false;
}
return true;
}
/**
* 检测文件是否为真实图片
* @return bool
*/
protected function checkTrueImg() {
if($this->imgFlag) {
if(!@getimagesize($this->fileInfo['tmp_name'])) {
$this->error = '011';
return false;
}
}
return true;
}
/**
* 检测是否通过HTTP Post方式上传过来的
* @return bool
*/
protected function checkHTTPPost() {
if(!is_uploaded_file($this->fileInfo['tmp_name'])) {
$this->error = '012';
return false;
}
return true;
}
/**
* 显示错误
*/
protected function showError() {
$e = $this->err[$this->error];
exit('<span style="color:red">' . $e . '</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->uniName = $this->getUniName();
$this->destination = $this->uploadPath . '/' . $this->uniName . '.' . $this->ext;
if(@move_uploaded_file($this->fileInfo['tmp_name'], $this->destination)) {
return $this->destination;
} else {
$this->error = '013';
$this->showError();
}
} else {
$this->showError();
}
}
}
- <?php
- /**
- * Created by PhpStorm.
- * User: DreamBoy
- * Date: 2016/4/9
- * Time: 10:31
- */
- header('content-type:text/html;charset=utf-8');
- require_once 'Upload.class.php';
- //$upload = new Upload();
- //$upload = new Upload('myFile2');
- $upload = new Upload('myFile2','uploads2');
- $dest = $upload->uploadFile();
- echo $dest;
<?php
/**
* Created by PhpStorm.
* User: DreamBoy
* Date: 2016/4/9
* Time: 10:31
*/
header('content-type:text/html;charset=utf-8');
require_once 'Upload.class.php';
//$upload = new Upload();
//$upload = new Upload('myFile2');
$upload = new Upload('myFile2','uploads2');
$dest = $upload->uploadFile();
echo $dest;