标签:
使用php 进行多文件的上传是学习php 必不可少的内容,因此我根据 慕课网上的教程改编了下,形成下面的代码,如若有什么问题,请指点指点我这个小菜鸟!
upload.class.php: 面向对象的类文件的封装
<?php /** * Created in upload. * FileName: upload.class.php * User: fanfan * Date: 2015/7/30 * Time: 21:26 */ //多文件上传封装 class upload { //protected $fileName; protected $maxSize; protected $allowMime; protected $allowExt; protected $uploadPath; protected $imgFlag; protected $fileInfo; protected $error; protected $ext; protected $destination; /** * 定义构造函数 * @param string $fileName * @param bool|true $imgFlag * @param string $uploadPath * @param int $maxSize * @param array $allowExt * @param array $allowMime */ public function __construct($fileInfo=null, $imgFlag = true, $uploadPath = ‘./uploads‘, $maxSize = 5242880, $allowExt = array(‘jpeg‘, ‘jpg‘, ‘png‘, ‘gif‘), $allowMime = array(‘image/jpeg‘, ‘image/jpg‘, ‘image/png‘, ‘image/gif‘)) { //$this->fileName = $fileName; $this->maxSize = $maxSize; $this->allowMime = $allowMime; $this->allowExt = $allowExt; $this->uploadPath = $uploadPath; $this->imgFlag = $imgFlag; $this->fileInfo = $fileInfo; } public function getFiles(){ $i=0; foreach($_FILES as $file){ if(is_string($file[‘name‘])){ $files[$i]=$file; $i++; }elseif(is_array($file[‘name‘])){ foreach($file[‘name‘] as $key=>$val){ $files[$i][‘name‘]=$file[‘name‘][$key]; $files[$i][‘type‘]=$file[‘type‘][$key]; $files[$i][‘tmp_name‘]=$file[‘tmp_name‘][$key]; $files[$i][‘error‘]=$file[‘error‘][$key]; $files[$i][‘size‘]=$file[‘size‘][$key]; $i++; } } } return $files; } /** * 检测上传文件是否出错 * @return bool */ 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 { echo $this->fileInfo[‘name‘].‘文件上传出错‘; return false; } } /** * 检测文件大小 * @return bool */ protected function checkSize() { if ($this->fileInfo[‘size‘] > $this->maxSize) { $this->error = $this->fileInfo[‘name‘].‘上传的文件太大‘; return false; } return true; } /** * 检测文件上传方式 * @return bool */ protected function checkHTTPPost() { if (!is_uploaded_file($this->fileInfo[‘tmp_name‘])) { $this->error = $this->fileInfo[‘name‘].‘上传的文件不是用HTTP POST的方式上传的‘; return false; } return true; } /** * 检测扩展名 * @return bool */ protected function checkExt() { $this->ext = strtolower(pathinfo($this->fileInfo[‘name‘], PATHINFO_EXTENSION)); if (!in_array($this->ext, $this->allowExt)) { $this->error = $this->fileInfo[‘name‘].‘不允许的扩展名‘; return false; } return true; } /** * 检测文件类型 * @return bool */ protected function checkMime() { if (!in_array($this->fileInfo[‘type‘], $this->allowMime)) { $this->error = $this->fileInfo[‘name‘].‘不允许文件类型‘; return false; } return true; } /** * 检测是否是真实的图片 * @return bool */ protected function checkTrueImg() { if ($this->imgFlag) { if (!@getimagesize($this->fileInfo[‘tmp_name‘])) { $this->error = $this->fileInfo[‘name‘].‘不是真实的图片‘; return false; } } return true; } /** *检测目录是否存在则创建 */ protected function uploadPath(){ if(!file_exists($this->uploadPath)){ mkdir($this->uploadPath,0777,true); } } /** * 产生唯一的名字 * @return string */ protected function uniName(){ return md5(uniqid(microtime(true),true)); } /** * 上传文件 * @return string */ public function uploadFile() { if ($this->checkError() && $this->checkSize() && $this->checkHTTPPost() && $this->checkExt() && $this->checkMime() && $this->checkTrueImg()) { $this->uploadPath(); $this->destination=$this->uploadPath.‘/‘.$this->uniName().‘.‘.$this->ext; if(@move_uploaded_file($this->fileInfo[‘tmp_name‘],$this->destination)){ // return $this->fileInfo[‘name‘].$this->destination.‘上传成功‘; return $this->fileInfo[‘name‘].‘上传成功‘; }else{ $this->error=$this->fileInfo[‘name‘].‘文件移动失败‘; return $this->error; } } else { return $this->error; } } }
upload.php : 上传文件所使用的 php文件
<?php /** * Created in upload. * FileName: upload1.php * User: fanfan * Date: 2015/7/30 * Time: 22:39 */ ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>upload index</title> <link rel="stylesheet" href="bootstrap.css"> </head> <body> <div class="container"> <div class="row"> <form action="doAction.php" class="form" method="post" enctype="multipart/form-data"> <div class="group"> <label for="file">请上传文件</label> <input type="file" name="file[]" id="file"> <input type="file" name="file[]" id="file"> <!-- <input type="file" name="file" id="file">--> <!-- <input type="file" name="file" id="file" accept="image/png,image/jpg">--> <!-- <input type="hidden" name="MAX_FILE_SIZE" value="4000">--> </div> <button type="submit" class="btn btn-default">提交</button> </form> </div> </div> </body> </html>
doAction.php 执行upload.php 所上传的文件
<?php /** * Created in upload. * FileName: doAction.php * User: fanfan * Date: 2015/7/30 * Time: 22:39 */ header(‘content-type:text/html;charset=utf-8‘); require_once ‘upload.class1.php‘; $getFiles= new upload(); $files=$getFiles->getFiles(); //每上传一个文件使用一次类和方法,感觉有点问题,请看到代码的同学给我指点指点 foreach($files as $fileInfo){ $upload= new upload($fileInfo); echo ‘<hr />‘; print_r($upload); $dest=$upload->uploadFile(); echo $dest; echo ‘<hr />‘; }
标签:
原文地址:http://www.cnblogs.com/fengyeyang/p/4692776.html