PHP 图片上传工具类(支持多文件上传)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP 图片上传工具类(支持多文件上传),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6810字,纯文字阅读大概需要10分钟。
内容图文
![PHP 图片上传工具类(支持多文件上传)](/upload/InfoBanner/zyjiaocheng/1159/f947366d385c46e1a4b39e91797fe3db.jpg)
====================ImageUploadTool========================
![技术分享](/upload/getfiles/default/2022/11/10/20221110040140025.jpg)
![技术分享](/img/jian.gif)
<?php class ImageUploadTool { private$file; //文件信息private$fileList; //文件列表private$inputName; //标签名称private$uploadPath; //上传路径private$fileMaxSize; //最大尺寸private$uploadFiles; //上传文件 //允许上传的文件类型private$allowExt = array(‘bmp‘, ‘jpg‘, ‘jpeg‘, ‘png‘, ‘gif‘); /** * ImageUploadTool constructor. * @param $inputName input标签的name属性 * @param $uploadPath 文件上传路径 */publicfunction __construct($inputName, $uploadPath) { $this->inputName = $inputName; $this->uploadPath = $uploadPath; $this->fileList = array(); $this->file = $file = array( ‘name‘ => null, ‘type‘ => null, ‘tmp_name‘ => null, ‘size‘ => null, ‘errno‘ => null, ‘error‘ => null ); } /** * 设置允许上传的图片后缀格式 * @param $allowExt 文件后缀数组 */publicfunction setAllowExt($allowExt) { if (is_array($allowExt)) { $this->allowExt = $allowExt; } else { $this->allowExt = array($allowExt); } } /** * 设置允许上传的图片规格 * @param $fileMaxSize 最大文件尺寸 */publicfunction setMaxSize($fileMaxSize) { $this->fileMaxSize = $fileMaxSize; } /** * 获取上传成功的文件数组 * @return mixed */publicfunction getUploadFiles() { return$this->uploadFiles; } /** * 得到文件上传的错误信息 * @return array|mixed */publicfunction getErrorMsg() { if (count($this->fileList) == 0) { return$this->file[‘error‘]; } else { $errArr = array(); foreach ($this->fileList as$item) { array_push($errArr, $item[‘error‘]); } return$errArr; } } /** * 初始化文件参数 * @param $isList */privatefunction initFile($isList) { if ($isList) { foreach ($_FILES[$this->inputName] as$key => $item) { for ($i = 0; $i < count($item); $i++) { if ($key == ‘error‘) { $this->fileList[$i][‘error‘] = null; $this->fileList[$i][‘errno‘] = $item[$i]; } else { $this->fileList[$i][$key] = $item[$i]; } } } } else { $this->file[‘name‘] = $_FILES[$this->inputName][‘name‘]; $this->file[‘type‘] = $_FILES[$this->inputName][‘type‘]; $this->file[‘tmp_name‘] = $_FILES[$this->inputName][‘tmp_name‘]; $this->file[‘size‘] = $_FILES[$this->inputName][‘size‘]; $this->file[‘errno‘] = $_FILES[$this->inputName][‘error‘]; } } /** * 上传错误检查 * @param $errno * @return null|string */privatefunction errorCheck($errno) { switch ($errno) { case UPLOAD_ERR_OK: returnnull; case UPLOAD_ERR_INI_SIZE: return ‘文件尺寸超过服务器限制‘; case UPLOAD_ERR_FORM_SIZE: return ‘文件尺寸超过表单限制‘; case UPLOAD_ERR_PARTIAL: return ‘只有部分文件被上传‘; case UPLOAD_ERR_NO_FILE: return ‘没有文件被上传‘; case UPLOAD_ERR_NO_TMP_DIR: return ‘找不到临时文件夹‘; case UPLOAD_ERR_CANT_WRITE: return ‘文件写入失败‘; case UPLOAD_ERR_EXTENSION: return ‘上传被扩展程序中断‘; } } /** * 上传文件校验 * @param $file * @throws Exception */privatefunction fileCheck($file) { //图片上传过程是否顺利if ($file[‘errno‘] != 0) { $error = $this->errorCheck($file[‘errno‘]); thrownewException($error); } //图片尺寸是否符合要求if (!empty($this->fileMaxSize) && $file[‘size‘] > $this->fileMaxSize) { thrownewException(‘文件尺寸超过‘ . ($this->fileMaxSize / 1024) . ‘KB‘); } //图片类型是否符合要求$ext = pathinfo($file[‘name‘], PATHINFO_EXTENSION); if (!in_array($ext, $this->allowExt)) { thrownewException(‘不符合要求的文件类型‘); } //图片上传方式是否为HTTPif (!is_uploaded_file($file[‘tmp_name‘])) { thrownewException(‘文件不是通过HTTP方式上传的‘); } //图片是否可以读取if (!getimagesize($file[‘tmp_name‘])) { thrownewException(‘图片文件损坏‘); } //检查上传路径是否存在if (!file_exists($this->uploadPath)) { mkdir($this->uploadPath, null, true); } } /** * 单文件上传,成功返回true * @return bool */publicfunction acceptSingleFile() { $this->initFile(false); try { $this->fileCheck($this->file); $md_name = md5(uniqid(microtime(true), true)) . ‘.‘ . pathinfo($this->file[‘name‘], PATHINFO_EXTENSION); if (move_uploaded_file($this->file[‘tmp_name‘], $this->uploadPath . $md_name)) { $this->uploadFiles = array($this->uploadPath . $md_name); } else { thrownewException(‘文件上传失败‘); } } catch (Exception$e) { $this->file[‘error‘] = $e->getMessage(); } finally { if (file_exists($this->file[‘tmp_name‘])) { unlink($this->file[‘tmp_name‘]); } } returnempty($this->file[‘error‘]) ? true : false; } /** * 多文件上传,全部成功返回true * @return bool */publicfunction acceptMultiFile() { $this->initFile(true); $this->uploadFiles = array(); for ($i = 0; $i < count($this->fileList); $i++) { try { $this->fileCheck($this->fileList[$i]); $ext = pathinfo($this->fileList[$i][‘name‘], PATHINFO_EXTENSION); $md_name = md5(uniqid(microtime(true), true)) . ‘.‘ . $ext; if (move_uploaded_file($this->fileList[$i][‘tmp_name‘], $this->uploadPath . $md_name)) { array_push($this->uploadFiles, $this->uploadPath . $md_name); } else { thrownewException(‘文件上传失败‘); } } catch (Exception$e) { $this->fileList[$i][‘error‘] = $e->getMessage(); } finally { if (file_exists($this->fileList[$i][‘tmp_name‘])) { unlink($this->fileList[$i][‘tmp_name‘]); } } } foreach ($this->fileList as$item) { if (!empty($item[‘error‘])) { returnfalse; } } returntrue; } }
=======================单文件上传===========================
首先创建一个简单HTML表单:
< form action ="tool_test.php" method ="post" enctype ="multipart/form-data" > <!-- 通过表单隐藏域限制上传文件的最大值 --> <!-- <input type="hidden" name="MAX_FILE_SIZE" value="204800"> --> <!-- 通过accept属性限制上传文件类型 --> <!-- <input type="hidden" name="myfile" accept="image/jpeg,image/png"> --> < label for ="upload" >请选择上传的文件</label><input type="file" id="upload" name="myfile"><br><input type="submit" value="上传文件"></form>
再创建一个tool_test.php文件:
<?php //引用图片上传工具require_once ‘ImageUploadTool.class.php‘; //初始化上传工具,参数(input表单name属性 , 文件上传路径)$uploadTool = new ImageUploadTool(‘myfile‘, ‘file/‘); //进行单文件上传操作,上传成功返回trueif ($uploadTool->acceptSingleFile()) { //获取上传后的文件路径,并用img标签显示echo "<img src=‘{$uploadTool->getUploadFiles()[0]}‘/>"; } else { //打印错误信息echo$uploadTool->getErrorMsg(); }
=======================多文件上传===========================
首先创建一个简单HTML表单:
需要在<input type="file">标签的name属性后面追加“[]”,并且添加 multiple 属性
< form action ="tool_test.php" method ="post" enctype ="multipart/form-data" > < label for ="upload" >请选择上传的文件</label><input type="file" id="upload" name="myfile[]" multiple><br><input type="submit" value="上传文件"></form>
再创建一个tool_test.php文件:
<?php //引用图片上传工具require_once ‘ImageUploadTool.class.php‘; //初始化上传工具,参数(input表单name属性 , 文件上传路径)$uploadTool = new ImageUploadTool(‘myfile‘, ‘file/‘); //设置允许上传的文件后缀类型$uploadTool->setAllowExt(array(‘png‘, ‘jpg‘)); //设置上传文件的最大尺寸$uploadTool->setMaxSize(1024 * 200); //进行多文件上传操作,全部上传成功返回true$uploadTool->acceptMultiFile(); //打印所有上传成功的图片路径print_r($uploadTool->getUploadFiles()); //打印所有上传失败的错误信息print_r($uploadTool->getErrorMsg());
多文件上传测试:
原文:http://www.cnblogs.com/woider/p/5847821.html
内容总结
以上是互联网集市为您收集整理的PHP 图片上传工具类(支持多文件上传)全部内容,希望文章能够帮你解决PHP 图片上传工具类(支持多文件上传)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。