Sivo 悉见

开发 Composer 包详细步骤

匿名 · 更新于 2019/1/30

开发一个 composer 通用文件上传包,发布到 Packagist,并在 Laravel 中测试。

一、GitHub 创建一个名 uploadfile 新仓库,并克隆至本地。

$ git clone git@github.com:guanguans/uploadfile.git
$ cd uploadfile

二、初始化项目,生成composer.json文件

2.1 步骤

yzm@Alert MINGW64 /i/phpstudy/WWW/uploadfile
$ composer init

Welcome to the Composer config generator

This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [yzm/try-make-package]: guanguans/uploadfile Description []: 一个通用文件上传包 Author [guanguans <53222411@qq.com>, n to skip]: guanguans <yzmguanguan@gmail.com> Minimum Stability []: dev Package Type (e.g. library, project, metapackage, composer-plugin) []: l ibrary License []: MIT

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes] ? yes Search for a package: php Enter the version constraint to require (or leave blank to use the lates t version): >=5.4.0 Search for a package: Would you like to define your dev dependencies (require-dev) interactive ly [yes]? yes Search for a package: php Enter the version constraint to require (or leave blank to use the lates t version): >=5.4.0 Search for a package:

{ "name": "guanguans/uploadfile", "description": "一个通用文件上传包", "type": "library", "require": { "php": ">=5.4" }, "require-dev": { "php": ">=5.4" }, "license": "MIT", "authors": [ { "name": "guanguans", "email": "yzmguanguan@gmail.com" } ], "minimum-stability": "dev" }

Do you confirm generation [yes]? yes

2.2 步骤解释

yzm@Alert MINGW64 /i/phpstudy/WWW/uploadfile
$ composer init

Welcome to the Composer config generator

This command will guide you through creating your composer.json config.

// 1. 输入项目命名空间 // 注意<vendor>/<name> 必须要符合 [a-z0-9_.-]+/[a-z0-9_.-]+ Package name (<vendor>/<name>) [dell/htdocs]: yourname/projectname

// 2. 项目描述 Description []: 这是一个测试

// 3. 输入作者信息,可以直接回车 Author [guanguans <53222411@qq.com>, n to skip]:

// 4. 输入最低稳定版本,stable, RC, beta, alpha, dev Minimum Stability []: dev

// 5. 输入项目类型, Package Type (e.g. library, project, metapackage, composer-plugin) []: library

// 6. 输入授权类型 License []: > Define your dependencies.

// 7. 输入依赖信息 Would you like to define your dependencies (require) interactively [yes]?

// 如果需要依赖,则输入要安装的依赖 Search for a package: php

// 输入版本号 Enter the version constraint to require (or leave blank to use the latest version): >=5.4.0

// 如需多个,则重复以上两个步骤

// 8. 是否需要require-dev, Would you like to define your dev dependencies (require-dev) interactively [yes]?

// 操作同上 { "name": "guanguans/uploadfile", "description": "一个通用文件上传包", "type": "library", "require": { "php": ">=5.4" }, "require-dev": { "php": ">=5.4" }, "license": "MIT", "authors": [ { "name": "guanguans", "email": "yzmguanguan@gmail.com" } ], "minimum-stability": "dev" }

// 9. 是否生成composer.json Do you confirm generation [yes]? yes

三、添加自动加载

在上一步生成的composer.json中追加

"autoload": {
    "psr-4": {
        "Guanguans\\": "src/"
    }
}

四、构建项目

4.1 新建uploadfile/src/UploadFile.php

├─uploadfile                
│  ├─src                     
│  │  ├─UploadFile.php
│  └─composer.json
<?php
/**
 * 通用文件上传类
 * @author  guanguans <yzmguanguan@gmail.com>
 */
namespace Guanguans;  // 注意命名空间与 composer.json 中的一致

class UploadFile
{
    private $config = [   
        'maxSize'           =>  -1,    // 上传文件的最大值
        'supportMulti'      =>  true,    // 是否支持多文件上传
        'allowExts'         =>  [],    // 允许上传的文件后缀 留空不作后缀检查
        'allowTypes'        =>  [],    // 允许上传的文件类型 留空不做检查
        'thumb'             =>  false,    // 使用对上传图片进行缩略图处理
        'imageClassPath'    =>  'ORG.Util.Image',    // 图库类包路径
        'thumbMaxWidth'     =>  '',// 缩略图最大宽度
        'thumbMaxHeight'    =>  '',// 缩略图最大高度
        'thumbPrefix'       =>  'thumb_',// 缩略图前缀
        'thumbSuffix'       =>  '',
        'thumbPath'         =>  '',// 缩略图保存路径
        'thumbFile'         =>  '',// 缩略图文件名
        'thumbExt'          =>  '',// 缩略图扩展名
        'thumbRemoveOrigin' =>  false,// 是否移除原图
        'thumbType'         =>  1, // 缩略图生成方式 1 按设置大小截取 0 按原图等比例缩略
        'zipImages'         =>  false,// 压缩图片文件上传
        'autoSub'           =>  false,// 启用子目录保存文件
        'subType'           =>  'hash',// 子目录创建方式 可以使用hash date custom
        'subDir'            =>  '', // 子目录名称 subType为custom方式后有效
        'dateFormat'        =>  'Ymd',
        'hashLevel'         =>  1, // hash的目录层次
        'savePath'          =>  '',// 上传文件保存路径
        'autoCheck'         =>  true, // 是否自动检查附件
        'uploadReplace'     =>  false,// 存在同名是否覆盖
        'saveRule'          =>  'uniqid',// 上传文件命名规则
        'hashType'          =>  'md5_file',// 上传文件Hash规则函数名
    ];
<span class="c1" style="color: rgb(117, 113, 94);">// 错误信息

private $error = ''; // 上传成功的文件信息 private $uploadFileInfo ;

<span class="k" style="color: rgb(102, 217, 239);">public</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">__get</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$name</span><span class="p" style="color: rgb(248, 248, 242);">){</span>
    <span class="k" style="color: rgb(102, 217, 239);">if</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nb" style="color: rgb(248, 248, 242);">isset</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">config</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="nv" style="color: rgb(248, 248, 242);">$name</span><span class="p" style="color: rgb(248, 248, 242);">]))</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
        <span class="k" style="color: rgb(102, 217, 239);">return</span> <span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">config</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="nv" style="color: rgb(248, 248, 242);">$name</span><span class="p" style="color: rgb(248, 248, 242);">];</span>
    <span class="p" style="color: rgb(248, 248, 242);">}</span>
    <span class="k" style="color: rgb(102, 217, 239);">return</span> <span class="kc" style="color: rgb(102, 217, 239);">null</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
<span class="p" style="color: rgb(248, 248, 242);">}</span>

<span class="k" style="color: rgb(102, 217, 239);">public</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">__set</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$name</span><span class="p" style="color: rgb(248, 248, 242);">,</span><span class="nv" style="color: rgb(248, 248, 242);">$value</span><span class="p" style="color: rgb(248, 248, 242);">){</span>
    <span class="k" style="color: rgb(102, 217, 239);">if</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nb" style="color: rgb(248, 248, 242);">isset</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">config</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="nv" style="color: rgb(248, 248, 242);">$name</span><span class="p" style="color: rgb(248, 248, 242);">]))</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
        <span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">config</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="nv" style="color: rgb(248, 248, 242);">$name</span><span class="p" style="color: rgb(248, 248, 242);">]</span>    <span class="o" style="color: rgb(249, 38, 114);">=</span>   <span class="nv" style="color: rgb(248, 248, 242);">$value</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
    <span class="p" style="color: rgb(248, 248, 242);">}</span>
<span class="p" style="color: rgb(248, 248, 242);">}</span>

<span class="k" style="color: rgb(102, 217, 239);">public</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">__isset</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$name</span><span class="p" style="color: rgb(248, 248, 242);">){</span>
    <span class="k" style="color: rgb(102, 217, 239);">return</span> <span class="nb" style="color: rgb(248, 248, 242);">isset</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">config</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="nv" style="color: rgb(248, 248, 242);">$name</span><span class="p" style="color: rgb(248, 248, 242);">]);</span>
<span class="p" style="color: rgb(248, 248, 242);">}</span>

<span class="sd" style="color: rgb(230, 219, 116);">/**
 * 架构函数
 * @access public
 * @param array $config  上传参数
 */</span>
<span class="k" style="color: rgb(102, 217, 239);">public</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">__construct</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$config</span><span class="o" style="color: rgb(249, 38, 114);">=</span><span class="p" style="color: rgb(248, 248, 242);">[])</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
    <span class="k" style="color: rgb(102, 217, 239);">if</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nb" style="color: rgb(248, 248, 242);">is_array</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$config</span><span class="p" style="color: rgb(248, 248, 242);">))</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
        <span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">config</span>   <span class="o" style="color: rgb(249, 38, 114);">=</span>   <span class="nb" style="color: rgb(248, 248, 242);">array_merge</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">config</span><span class="p" style="color: rgb(248, 248, 242);">,</span><span class="nv" style="color: rgb(248, 248, 242);">$config</span><span class="p" style="color: rgb(248, 248, 242);">);</span>
    <span class="p" style="color: rgb(248, 248, 242);">}</span>
<span class="p" style="color: rgb(248, 248, 242);">}</span>

<span class="sd" style="color: rgb(230, 219, 116);">/**
 * 上传一个文件
 * @access public
 * @param mixed $name 数据
 * @param string $value  数据表名
 * @return string
 */</span>
<span class="k" style="color: rgb(102, 217, 239);">private</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">save</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$file</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
    <span class="nv" style="color: rgb(248, 248, 242);">$filename</span> <span class="o" style="color: rgb(249, 38, 114);">=</span> <span class="nv" style="color: rgb(248, 248, 242);">$file</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="s1" style="color: rgb(230, 219, 116);">&#39;savepath&#39;</span><span class="p" style="color: rgb(248, 248, 242);">]</span><span class="o" style="color: rgb(249, 38, 114);">.</span><span class="nv" style="color: rgb(248, 248, 242);">$file</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="s1" style="color: rgb(230, 219, 116);">&#39;savename&#39;</span><span class="p" style="color: rgb(248, 248, 242);">];</span>
    <span class="k" style="color: rgb(102, 217, 239);">if</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="o" style="color: rgb(249, 38, 114);">!</span><span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">uploadReplace</span> <span class="o" style="color: rgb(249, 38, 114);">&amp;&amp;</span> <span class="nb" style="color: rgb(248, 248, 242);">is_file</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$filename</span><span class="p" style="color: rgb(248, 248, 242);">))</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
        <span class="c1" style="color: rgb(117, 113, 94);">// 不覆盖同名文件

$this->error = '文件已经存在!'.$filename; return false; } // 如果是图像文件 检测文件格式 if( in_array(strtolower($file['extension']), ['gif','jpg','jpeg','bmp','png','swf'])) { $info = getimagesize($file['tmp_name']); if(false === $info || ('gif' == strtolower($file['extension']) && empty($info['bits']))){ $this->error = '非法图像文件'; return false; } } if(!move_uploaded_file($file['tmp_name'], $this->autoCharset($filename,'utf-8','gbk'))) { $this->error = '文件上传保存错误!'; return false; } if($this->thumb && in_array(strtolower($file['extension']), ['gif','jpg','jpeg','bmp','png'])) { $image = getimagesize($filename); if(false !== $image) { //是图像文件生成缩略图 $thumbWidth = explode(',',$this->thumbMaxWidth); $thumbHeight = explode(',',$this->thumbMaxHeight); $thumbPrefix = explode(',',$this->thumbPrefix); $thumbSuffix = explode(',',$this->thumbSuffix); $thumbFile = explode(',',$this->thumbFile); $thumbPath = $this->thumbPath?$this->thumbPath:dirname($filename).'/'; $thumbExt = $this->thumbExt ? $this->thumbExt : $file['extension']; //自定义缩略图扩展名 // 生成图像缩略图 import($this->imageClassPath); for($i=0,$len=count($thumbWidth); $i<$len; $i++) { if(!empty($thumbFile[$i])) { $thumbname = $thumbFile[$i]; }else{ $prefix = isset($thumbPrefix[$i])?$thumbPrefix[$i]:$thumbPrefix[0]; $suffix = isset($thumbSuffix[$i])?$thumbSuffix[$i]:$thumbSuffix[0]; $thumbname = $prefix.basename($filename,'.'.$file['extension']).$suffix; } if(1 == $this->thumbType){ Image::thumb2($filename,$thumbPath.$thumbname.'.'.$thumbExt,'',$thumbWidth[$i],$thumbHeight[$i],true); }else{ Image::thumb($filename,$thumbPath.$thumbname.'.'.$thumbExt,'',$thumbWidth[$i],$thumbHeight[$i],true); }

            <span class="p" style="color: rgb(248, 248, 242);">}</span>
            <span class="k" style="color: rgb(102, 217, 239);">if</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">thumbRemoveOrigin</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
                <span class="c1" style="color: rgb(117, 113, 94);">// 生成缩略图之后删除原图

unlink($filename); } } } if($this->zipImags) { // TODO 对图片压缩包在线解压 } return true; }

<span class="sd" style="color: rgb(230, 219, 116);">/**
 * 上传所有文件
 * @access public
 * @param string $savePath  上传文件保存路径
 * @return string
 */</span>
<span class="k" style="color: rgb(102, 217, 239);">public</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">upload</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$savePath</span> <span class="o" style="color: rgb(249, 38, 114);">=</span><span class="s1" style="color: rgb(230, 219, 116);">&#39;&#39;</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
    <span class="c1" style="color: rgb(117, 113, 94);">//如果不指定保存文件名,则由系统默认

if(empty($savePath)) $savePath = $this->savePath; // 检查上传目录 if(!is_dir($savePath)) { // 检查目录是否编码后的 if(is_dir(base64_decode($savePath))) { $savePath = base64_decode($savePath); }else{ // 尝试创建目录 if(!mkdir($savePath)){ $this->error = '上传目录'.$savePath.'不存在'; return false; } } }else { if(!is_writeable($savePath)) { $this->error = '上传目录'.$savePath.'不可写'; return false; } } $fileInfo = []; $isUpload = false;

    <span class="c1" style="color: rgb(117, 113, 94);">// 获取上传的文件信息

// 对$_FILES数组信息处理 $files = $this->dealFiles($_FILES); foreach($files as $key => $file) { //过滤无效的上传 if(!empty($file['name'])) { //登记上传文件的扩展信息 if(!isset($file['key'])) $file['key'] = $key; $file['extension'] = $this->getExt($file['name']); $file['savepath'] = $savePath; $file['savename'] = $this->getSaveName($file);

            <span class="c1" style="color: rgb(117, 113, 94);">// 自动检查附件

if($this->autoCheck) { if(!$this->check($file)) return false; }

            <span class="c1" style="color: rgb(117, 113, 94);">//保存上传文件

if(!$this->save($file)) return false; if(function_exists($this->hashType)) { $fun = $this->hashType; $file['hash'] = $fun($this->autoCharset($file['savepath'].$file['savename'],'utf-8','gbk')); } //上传成功后保存文件信息,供其他地方调用 unset($file['tmp_name'],$file['error']); $fileInfo[] = $file; $isUpload = true; } } if($isUpload) { $this->uploadFileInfo = $fileInfo; return true; }else { $this->error = '没有选择上传文件'; return false; } }

<span class="sd" style="color: rgb(230, 219, 116);">/**
 * 上传单个上传字段中的文件 支持多附件
 * @access public
 * @param array $file  上传文件信息
 * @param string $savePath  上传文件保存路径
 * @return string
 */</span>
<span class="k" style="color: rgb(102, 217, 239);">public</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">uploadOne</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$file</span><span class="p" style="color: rgb(248, 248, 242);">,</span><span class="nv" style="color: rgb(248, 248, 242);">$savePath</span><span class="o" style="color: rgb(249, 38, 114);">=</span><span class="s1" style="color: rgb(230, 219, 116);">&#39;&#39;</span><span class="p" style="color: rgb(248, 248, 242);">){</span>
    <span class="c1" style="color: rgb(117, 113, 94);">//如果不指定保存文件名,则由系统默认

if(empty($savePath)) $savePath = $this->savePath; // 检查上传目录 if(!is_dir($savePath)) { // 尝试创建目录 if(!mkdir($savePath,0777,true)){ $this->error = '上传目录'.$savePath.'不存在'; return false; } }else { if(!is_writeable($savePath)) { $this->error = '上传目录'.$savePath.'不可写'; return false; } } //过滤无效的上传 if(!empty($file['name'])) { $fileArray = []; if(is_array($file['name'])) { $keys = array_keys($file); $count = count($file['name']); for ($i=0; $i<$count; $i++) { foreach ($keys as $key) $fileArray[$i][$key] = $file[$key][$i]; } }else{ $fileArray[] = $file; } $info = []; foreach ($fileArray as $key=>$file){ //登记上传文件的扩展信息 $file['extension'] = $this->getExt($file['name']); $file['savepath'] = $savePath; $file['savename'] = $this->getSaveName($file); // 自动检查附件 if($this->autoCheck) { if(!$this->check($file)) return false; } //保存上传文件 if(!$this->save($file)) return false; if(function_exists($this->hashType)) { $fun = $this->hashType; $file['hash'] = $fun($this->autoCharset($file['savepath'].$file['savename'],'utf-8','gbk')); } unset($file['tmp_name'],$file['error']); $info[] = $file; } // 返回上传的文件信息 return $info; }else { $this->error = '没有选择上传文件'; return false; } }

<span class="sd" style="color: rgb(230, 219, 116);">/**
 * 转换上传文件数组变量为正确的方式
 * @access private
 * @param array $files  上传的文件变量
 * @return array
 */</span>
<span class="k" style="color: rgb(102, 217, 239);">private</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">dealFiles</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$files</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
    <span class="nv" style="color: rgb(248, 248, 242);">$fileArray</span>  <span class="o" style="color: rgb(249, 38, 114);">=</span> <span class="p" style="color: rgb(248, 248, 242);">[];</span>
    <span class="nv" style="color: rgb(248, 248, 242);">$n</span>          <span class="o" style="color: rgb(249, 38, 114);">=</span> <span class="mi" style="color: rgb(174, 129, 255);">0</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
    <span class="k" style="color: rgb(102, 217, 239);">foreach</span> <span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$files</span> <span class="k" style="color: rgb(102, 217, 239);">as</span> <span class="nv" style="color: rgb(248, 248, 242);">$key</span><span class="o" style="color: rgb(249, 38, 114);">=&gt;</span><span class="nv" style="color: rgb(248, 248, 242);">$file</span><span class="p" style="color: rgb(248, 248, 242);">){</span>
        <span class="k" style="color: rgb(102, 217, 239);">if</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nb" style="color: rgb(248, 248, 242);">is_array</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$file</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="s1" style="color: rgb(230, 219, 116);">&#39;name&#39;</span><span class="p" style="color: rgb(248, 248, 242);">]))</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
            <span class="nv" style="color: rgb(248, 248, 242);">$keys</span>       <span class="o" style="color: rgb(249, 38, 114);">=</span>   <span class="nb" style="color: rgb(248, 248, 242);">array_keys</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$file</span><span class="p" style="color: rgb(248, 248, 242);">);</span>
            <span class="nv" style="color: rgb(248, 248, 242);">$count</span>      <span class="o" style="color: rgb(249, 38, 114);">=</span>   <span class="nb" style="color: rgb(248, 248, 242);">count</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$file</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="s1" style="color: rgb(230, 219, 116);">&#39;name&#39;</span><span class="p" style="color: rgb(248, 248, 242);">]);</span>
            <span class="k" style="color: rgb(102, 217, 239);">for</span> <span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$i</span><span class="o" style="color: rgb(249, 38, 114);">=</span><span class="mi" style="color: rgb(174, 129, 255);">0</span><span class="p" style="color: rgb(248, 248, 242);">;</span> <span class="nv" style="color: rgb(248, 248, 242);">$i</span><span class="o" style="color: rgb(249, 38, 114);">&lt;</span><span class="nv" style="color: rgb(248, 248, 242);">$count</span><span class="p" style="color: rgb(248, 248, 242);">;</span> <span class="nv" style="color: rgb(248, 248, 242);">$i</span><span class="o" style="color: rgb(249, 38, 114);">++</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
                <span class="nv" style="color: rgb(248, 248, 242);">$fileArray</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="nv" style="color: rgb(248, 248, 242);">$n</span><span class="p" style="color: rgb(248, 248, 242);">][</span><span class="s1" style="color: rgb(230, 219, 116);">&#39;key&#39;</span><span class="p" style="color: rgb(248, 248, 242);">]</span> <span class="o" style="color: rgb(249, 38, 114);">=</span> <span class="nv" style="color: rgb(248, 248, 242);">$key</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
                <span class="k" style="color: rgb(102, 217, 239);">foreach</span> <span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$keys</span> <span class="k" style="color: rgb(102, 217, 239);">as</span> <span class="nv" style="color: rgb(248, 248, 242);">$_key</span><span class="p" style="color: rgb(248, 248, 242);">){</span>
                    <span class="nv" style="color: rgb(248, 248, 242);">$fileArray</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="nv" style="color: rgb(248, 248, 242);">$n</span><span class="p" style="color: rgb(248, 248, 242);">][</span><span class="nv" style="color: rgb(248, 248, 242);">$_key</span><span class="p" style="color: rgb(248, 248, 242);">]</span> <span class="o" style="color: rgb(249, 38, 114);">=</span> <span class="nv" style="color: rgb(248, 248, 242);">$file</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="nv" style="color: rgb(248, 248, 242);">$_key</span><span class="p" style="color: rgb(248, 248, 242);">][</span><span class="nv" style="color: rgb(248, 248, 242);">$i</span><span class="p" style="color: rgb(248, 248, 242);">];</span>
                <span class="p" style="color: rgb(248, 248, 242);">}</span>
                <span class="nv" style="color: rgb(248, 248, 242);">$n</span><span class="o" style="color: rgb(249, 38, 114);">++</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
            <span class="p" style="color: rgb(248, 248, 242);">}</span>
        <span class="p" style="color: rgb(248, 248, 242);">}</span><span class="k" style="color: rgb(102, 217, 239);">else</span><span class="p" style="color: rgb(248, 248, 242);">{</span>
           <span class="nv" style="color: rgb(248, 248, 242);">$fileArray</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="nv" style="color: rgb(248, 248, 242);">$key</span><span class="p" style="color: rgb(248, 248, 242);">]</span> <span class="o" style="color: rgb(249, 38, 114);">=</span> <span class="nv" style="color: rgb(248, 248, 242);">$file</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
        <span class="p" style="color: rgb(248, 248, 242);">}</span>
    <span class="p" style="color: rgb(248, 248, 242);">}</span>
   <span class="k" style="color: rgb(102, 217, 239);">return</span> <span class="nv" style="color: rgb(248, 248, 242);">$fileArray</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
<span class="p" style="color: rgb(248, 248, 242);">}</span>

<span class="sd" style="color: rgb(230, 219, 116);">/**
 * 获取错误代码信息
 * @access public
 * @param string $errorNo  错误号码
 * @return void
 */</span>
<span class="k" style="color: rgb(102, 217, 239);">protected</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">error</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$errorNo</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
     <span class="k" style="color: rgb(102, 217, 239);">switch</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$errorNo</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
        <span class="k" style="color: rgb(102, 217, 239);">case</span> <span class="mi" style="color: rgb(174, 129, 255);">1</span><span class="o" style="color: rgb(249, 38, 114);">:</span>
            <span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">error</span> <span class="o" style="color: rgb(249, 38, 114);">=</span> <span class="s1" style="color: rgb(230, 219, 116);">&#39;上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值&#39;</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
            <span class="k" style="color: rgb(102, 217, 239);">break</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
        <span class="k" style="color: rgb(102, 217, 239);">case</span> <span class="mi" style="color: rgb(174, 129, 255);">2</span><span class="o" style="color: rgb(249, 38, 114);">:</span>
            <span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">error</span> <span class="o" style="color: rgb(249, 38, 114);">=</span> <span class="s1" style="color: rgb(230, 219, 116);">&#39;上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值&#39;</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
            <span class="k" style="color: rgb(102, 217, 239);">break</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
        <span class="k" style="color: rgb(102, 217, 239);">case</span> <span class="mi" style="color: rgb(174, 129, 255);">3</span><span class="o" style="color: rgb(249, 38, 114);">:</span>
            <span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">error</span> <span class="o" style="color: rgb(249, 38, 114);">=</span> <span class="s1" style="color: rgb(230, 219, 116);">&#39;文件只有部分被上传&#39;</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
            <span class="k" style="color: rgb(102, 217, 239);">break</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
        <span class="k" style="color: rgb(102, 217, 239);">case</span> <span class="mi" style="color: rgb(174, 129, 255);">4</span><span class="o" style="color: rgb(249, 38, 114);">:</span>
            <span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">error</span> <span class="o" style="color: rgb(249, 38, 114);">=</span> <span class="s1" style="color: rgb(230, 219, 116);">&#39;没有文件被上传&#39;</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
            <span class="k" style="color: rgb(102, 217, 239);">break</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
        <span class="k" style="color: rgb(102, 217, 239);">case</span> <span class="mi" style="color: rgb(174, 129, 255);">6</span><span class="o" style="color: rgb(249, 38, 114);">:</span>
            <span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">error</span> <span class="o" style="color: rgb(249, 38, 114);">=</span> <span class="s1" style="color: rgb(230, 219, 116);">&#39;找不到临时文件夹&#39;</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
            <span class="k" style="color: rgb(102, 217, 239);">break</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
        <span class="k" style="color: rgb(102, 217, 239);">case</span> <span class="mi" style="color: rgb(174, 129, 255);">7</span><span class="o" style="color: rgb(249, 38, 114);">:</span>
            <span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">error</span> <span class="o" style="color: rgb(249, 38, 114);">=</span> <span class="s1" style="color: rgb(230, 219, 116);">&#39;文件写入失败&#39;</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
            <span class="k" style="color: rgb(102, 217, 239);">break</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
        <span class="k" style="color: rgb(102, 217, 239);">default</span><span class="o" style="color: rgb(249, 38, 114);">:</span>
            <span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">error</span> <span class="o" style="color: rgb(249, 38, 114);">=</span> <span class="s1" style="color: rgb(230, 219, 116);">&#39;未知上传错误!&#39;</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
    <span class="p" style="color: rgb(248, 248, 242);">}</span>
    <span class="k" style="color: rgb(102, 217, 239);">return</span> <span class="p" style="color: rgb(248, 248, 242);">;</span>
<span class="p" style="color: rgb(248, 248, 242);">}</span>

<span class="sd" style="color: rgb(230, 219, 116);">/**
 * 根据上传文件命名规则取得保存文件名
 * @access private
 * @param string $filename 数据
 * @return string
 */</span>
<span class="k" style="color: rgb(102, 217, 239);">private</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">getSaveName</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$filename</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
    <span class="nv" style="color: rgb(248, 248, 242);">$rule</span> <span class="o" style="color: rgb(249, 38, 114);">=</span> <span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">saveRule</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
    <span class="k" style="color: rgb(102, 217, 239);">if</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="k" style="color: rgb(102, 217, 239);">empty</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$rule</span><span class="p" style="color: rgb(248, 248, 242);">))</span> <span class="p" style="color: rgb(248, 248, 242);">{</span><span class="c1" style="color: rgb(117, 113, 94);">//没有定义命名规则,则保持文件名不变

$saveName = $filename['name']; }else { if(function_exists($rule)) { //使用函数生成一个唯一文件标识号 $saveName = $rule().".".$filename['extension']; }else { //使用给定的文件名作为标识号 $saveName = $rule.".".$filename['extension']; } } if($this->autoSub) { // 使用子目录保存文件 $filename['savename'] = $saveName; $saveName = $this->getSubName($filename).$saveName; } return $saveName; }

<span class="sd" style="color: rgb(230, 219, 116);">/**
 * 获取子目录的名称
 * @access private
 * @param array $file  上传的文件信息
 * @return string
 */</span>
<span class="k" style="color: rgb(102, 217, 239);">private</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">getSubName</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$file</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
    <span class="k" style="color: rgb(102, 217, 239);">switch</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">subType</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
        <span class="k" style="color: rgb(102, 217, 239);">case</span> <span class="s1" style="color: rgb(230, 219, 116);">&#39;custom&#39;</span><span class="o" style="color: rgb(249, 38, 114);">:</span>
            <span class="nv" style="color: rgb(248, 248, 242);">$dir</span>    <span class="o" style="color: rgb(249, 38, 114);">=</span>   <span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">subDir</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
            <span class="k" style="color: rgb(102, 217, 239);">break</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
        <span class="k" style="color: rgb(102, 217, 239);">case</span> <span class="s1" style="color: rgb(230, 219, 116);">&#39;date&#39;</span><span class="o" style="color: rgb(249, 38, 114);">:</span>
            <span class="nv" style="color: rgb(248, 248, 242);">$dir</span>    <span class="o" style="color: rgb(249, 38, 114);">=</span>   <span class="nb" style="color: rgb(248, 248, 242);">date</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">dateFormat</span><span class="p" style="color: rgb(248, 248, 242);">,</span><span class="nb" style="color: rgb(248, 248, 242);">time</span><span class="p" style="color: rgb(248, 248, 242);">())</span><span class="o" style="color: rgb(249, 38, 114);">.</span><span class="s1" style="color: rgb(230, 219, 116);">&#39;/&#39;</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
            <span class="k" style="color: rgb(102, 217, 239);">break</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
        <span class="k" style="color: rgb(102, 217, 239);">case</span> <span class="s1" style="color: rgb(230, 219, 116);">&#39;hash&#39;</span><span class="o" style="color: rgb(249, 38, 114);">:</span>
        <span class="k" style="color: rgb(102, 217, 239);">default</span><span class="o" style="color: rgb(249, 38, 114);">:</span>
            <span class="nv" style="color: rgb(248, 248, 242);">$name</span>   <span class="o" style="color: rgb(249, 38, 114);">=</span>   <span class="nb" style="color: rgb(248, 248, 242);">md5</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$file</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="s1" style="color: rgb(230, 219, 116);">&#39;savename&#39;</span><span class="p" style="color: rgb(248, 248, 242);">]);</span>
            <span class="nv" style="color: rgb(248, 248, 242);">$dir</span>    <span class="o" style="color: rgb(249, 38, 114);">=</span>   <span class="s1" style="color: rgb(230, 219, 116);">&#39;&#39;</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
            <span class="k" style="color: rgb(102, 217, 239);">for</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$i</span><span class="o" style="color: rgb(249, 38, 114);">=</span><span class="mi" style="color: rgb(174, 129, 255);">0</span><span class="p" style="color: rgb(248, 248, 242);">;</span><span class="nv" style="color: rgb(248, 248, 242);">$i</span><span class="o" style="color: rgb(249, 38, 114);">&lt;</span><span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">hashLevel</span><span class="p" style="color: rgb(248, 248, 242);">;</span><span class="nv" style="color: rgb(248, 248, 242);">$i</span><span class="o" style="color: rgb(249, 38, 114);">++</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
                <span class="nv" style="color: rgb(248, 248, 242);">$dir</span>   <span class="o" style="color: rgb(249, 38, 114);">.=</span>  <span class="nv" style="color: rgb(248, 248, 242);">$name</span><span class="p" style="color: rgb(248, 248, 242);">{</span><span class="nv" style="color: rgb(248, 248, 242);">$i</span><span class="p" style="color: rgb(248, 248, 242);">}</span><span class="o" style="color: rgb(249, 38, 114);">.</span><span class="s1" style="color: rgb(230, 219, 116);">&#39;/&#39;</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
            <span class="p" style="color: rgb(248, 248, 242);">}</span>
            <span class="k" style="color: rgb(102, 217, 239);">break</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
    <span class="p" style="color: rgb(248, 248, 242);">}</span>
    <span class="k" style="color: rgb(102, 217, 239);">if</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="o" style="color: rgb(249, 38, 114);">!</span><span class="nb" style="color: rgb(248, 248, 242);">is_dir</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$file</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="s1" style="color: rgb(230, 219, 116);">&#39;savepath&#39;</span><span class="p" style="color: rgb(248, 248, 242);">]</span><span class="o" style="color: rgb(249, 38, 114);">.</span><span class="nv" style="color: rgb(248, 248, 242);">$dir</span><span class="p" style="color: rgb(248, 248, 242);">))</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
        <span class="nb" style="color: rgb(248, 248, 242);">mkdir</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$file</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="s1" style="color: rgb(230, 219, 116);">&#39;savepath&#39;</span><span class="p" style="color: rgb(248, 248, 242);">]</span><span class="o" style="color: rgb(249, 38, 114);">.</span><span class="nv" style="color: rgb(248, 248, 242);">$dir</span><span class="p" style="color: rgb(248, 248, 242);">,</span><span class="mo" style="color: rgb(174, 129, 255);">0777</span><span class="p" style="color: rgb(248, 248, 242);">,</span><span class="kc" style="color: rgb(102, 217, 239);">true</span><span class="p" style="color: rgb(248, 248, 242);">);</span>
    <span class="p" style="color: rgb(248, 248, 242);">}</span>
    <span class="k" style="color: rgb(102, 217, 239);">return</span> <span class="nv" style="color: rgb(248, 248, 242);">$dir</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
<span class="p" style="color: rgb(248, 248, 242);">}</span>

<span class="sd" style="color: rgb(230, 219, 116);">/**
 * 检查上传的文件
 * @access private
 * @param array $file 文件信息
 * @return boolean
 */</span>
<span class="k" style="color: rgb(102, 217, 239);">private</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">check</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$file</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
    <span class="k" style="color: rgb(102, 217, 239);">if</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$file</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="s1" style="color: rgb(230, 219, 116);">&#39;error&#39;</span><span class="p" style="color: rgb(248, 248, 242);">]</span><span class="o" style="color: rgb(249, 38, 114);">!==</span> <span class="mi" style="color: rgb(174, 129, 255);">0</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
        <span class="c1" style="color: rgb(117, 113, 94);">//文件上传失败

//捕获错误代码 $this->error($file['error']); return false; } //文件上传成功,进行自定义规则检查 //检查文件大小 if(!$this->checkSize($file['size'])) { $this->error = '上传文件大小不符!'; return false; }

    <span class="c1" style="color: rgb(117, 113, 94);">//检查文件Mime类型

if(!$this->checkType($file['type'])) { $this->error = '上传文件MIME类型不允许!'; return false; } //检查文件类型 if(!$this->checkExt($file['extension'])) { $this->error ='上传文件类型不允许'; return false; }

    <span class="c1" style="color: rgb(117, 113, 94);">//检查是否合法上传

if(!$this->checkUpload($file['tmp_name'])) { $this->error = '非法上传文件!'; return false; } return true; }

<span class="c1" style="color: rgb(117, 113, 94);">// 自动转换字符集 支持数组转换

private function autoCharset($fContents, $from='gbk', $to='utf-8') { $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from; $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to; if (strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents))) { //如果编码相同或者非字符串标量则不转换 return $fContents; } if (function_exists('mb_convert_encoding')) { return mb_convert_encoding($fContents, $to, $from); } elseif (function_exists('iconv')) { return iconv($from, $to, $fContents); } else { return $fContents; } }

<span class="sd" style="color: rgb(230, 219, 116);">/**
 * 检查上传的文件类型是否合法
 * @access private
 * @param string $type 数据
 * @return boolean
 */</span>
<span class="k" style="color: rgb(102, 217, 239);">private</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">checkType</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$type</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
    <span class="k" style="color: rgb(102, 217, 239);">if</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="o" style="color: rgb(249, 38, 114);">!</span><span class="k" style="color: rgb(102, 217, 239);">empty</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">allowTypes</span><span class="p" style="color: rgb(248, 248, 242);">))</span>
        <span class="k" style="color: rgb(102, 217, 239);">return</span> <span class="nb" style="color: rgb(248, 248, 242);">in_array</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nb" style="color: rgb(248, 248, 242);">strtolower</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$type</span><span class="p" style="color: rgb(248, 248, 242);">),</span><span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">allowTypes</span><span class="p" style="color: rgb(248, 248, 242);">);</span>
    <span class="k" style="color: rgb(102, 217, 239);">return</span> <span class="kc" style="color: rgb(102, 217, 239);">true</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
<span class="p" style="color: rgb(248, 248, 242);">}</span>


<span class="sd" style="color: rgb(230, 219, 116);">/**
 * 检查上传的文件后缀是否合法
 * @access private
 * @param string $ext 后缀名
 * @return boolean
 */</span>
<span class="k" style="color: rgb(102, 217, 239);">private</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">checkExt</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$ext</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
    <span class="k" style="color: rgb(102, 217, 239);">if</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="o" style="color: rgb(249, 38, 114);">!</span><span class="k" style="color: rgb(102, 217, 239);">empty</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">allowExts</span><span class="p" style="color: rgb(248, 248, 242);">))</span>
        <span class="k" style="color: rgb(102, 217, 239);">return</span> <span class="nb" style="color: rgb(248, 248, 242);">in_array</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nb" style="color: rgb(248, 248, 242);">strtolower</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$ext</span><span class="p" style="color: rgb(248, 248, 242);">),</span><span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">allowExts</span><span class="p" style="color: rgb(248, 248, 242);">,</span><span class="kc" style="color: rgb(102, 217, 239);">true</span><span class="p" style="color: rgb(248, 248, 242);">);</span>
    <span class="k" style="color: rgb(102, 217, 239);">return</span> <span class="kc" style="color: rgb(102, 217, 239);">true</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
<span class="p" style="color: rgb(248, 248, 242);">}</span>

<span class="sd" style="color: rgb(230, 219, 116);">/**
 * 检查文件大小是否合法
 * @access private
 * @param integer $size 数据
 * @return boolean
 */</span>
<span class="k" style="color: rgb(102, 217, 239);">private</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">checkSize</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$size</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
    <span class="k" style="color: rgb(102, 217, 239);">return</span> <span class="o" style="color: rgb(249, 38, 114);">!</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$size</span> <span class="o" style="color: rgb(249, 38, 114);">&gt;</span> <span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">maxSize</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="o" style="color: rgb(249, 38, 114);">||</span> <span class="p" style="color: rgb(248, 248, 242);">(</span><span class="o" style="color: rgb(249, 38, 114);">-</span><span class="mi" style="color: rgb(174, 129, 255);">1</span> <span class="o" style="color: rgb(249, 38, 114);">==</span> <span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">maxSize</span><span class="p" style="color: rgb(248, 248, 242);">);</span>
<span class="p" style="color: rgb(248, 248, 242);">}</span>

<span class="sd" style="color: rgb(230, 219, 116);">/**
 * 检查文件是否非法提交
 * @access private
 * @param string $filename 文件名
 * @return boolean
 */</span>
<span class="k" style="color: rgb(102, 217, 239);">private</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">checkUpload</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$filename</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
    <span class="k" style="color: rgb(102, 217, 239);">return</span> <span class="nb" style="color: rgb(248, 248, 242);">is_uploaded_file</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$filename</span><span class="p" style="color: rgb(248, 248, 242);">);</span>
<span class="p" style="color: rgb(248, 248, 242);">}</span>

<span class="sd" style="color: rgb(230, 219, 116);">/**
 * 取得上传文件的后缀
 * @access private
 * @param string $filename 文件名
 * @return boolean
 */</span>
<span class="k" style="color: rgb(102, 217, 239);">private</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">getExt</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$filename</span><span class="p" style="color: rgb(248, 248, 242);">)</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
    <span class="nv" style="color: rgb(248, 248, 242);">$pathinfo</span> <span class="o" style="color: rgb(249, 38, 114);">=</span> <span class="nb" style="color: rgb(248, 248, 242);">pathinfo</span><span class="p" style="color: rgb(248, 248, 242);">(</span><span class="nv" style="color: rgb(248, 248, 242);">$filename</span><span class="p" style="color: rgb(248, 248, 242);">);</span>
    <span class="k" style="color: rgb(102, 217, 239);">return</span> <span class="nv" style="color: rgb(248, 248, 242);">$pathinfo</span><span class="p" style="color: rgb(248, 248, 242);">[</span><span class="s1" style="color: rgb(230, 219, 116);">&#39;extension&#39;</span><span class="p" style="color: rgb(248, 248, 242);">];</span>
<span class="p" style="color: rgb(248, 248, 242);">}</span>

<span class="sd" style="color: rgb(230, 219, 116);">/**
 * 取得上传文件的信息
 * @access public
 * @return array
 */</span>
<span class="k" style="color: rgb(102, 217, 239);">public</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">getUploadFileInfo</span><span class="p" style="color: rgb(248, 248, 242);">()</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
    <span class="k" style="color: rgb(102, 217, 239);">return</span> <span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">uploadFileInfo</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
<span class="p" style="color: rgb(248, 248, 242);">}</span>

<span class="sd" style="color: rgb(230, 219, 116);">/**
 * 取得最后一次错误信息
 * @access public
 * @return string
 */</span>
<span class="k" style="color: rgb(102, 217, 239);">public</span> <span class="k" style="color: rgb(102, 217, 239);">function</span> <span class="nf" style="color: rgb(166, 226, 46);">getErrorMsg</span><span class="p" style="color: rgb(248, 248, 242);">()</span> <span class="p" style="color: rgb(248, 248, 242);">{</span>
    <span class="k" style="color: rgb(102, 217, 239);">return</span> <span class="nv" style="color: rgb(248, 248, 242);">$this</span><span class="o" style="color: rgb(249, 38, 114);">-&gt;</span><span class="na" style="color: rgb(166, 226, 46);">error</span><span class="p" style="color: rgb(248, 248, 242);">;</span>
<span class="p" style="color: rgb(248, 248, 242);">}</span>

}

4.2 测试

4.2.1 终端下执行 composer install,这时会生成vendor目录,及其他文件

yzm@Alert MINGW64 /i/phpstudy/WWW/uploadfile
$ composer install

4.2.2 新建uploadfile/test/UpploadFileTest.phpuploadfile/test/UpploadFile.html

  • UpploadFileTest.php
<?php

require_once '../vendor/autoload.php';

use Guanguans\UploadFile;

$upload = new UploadFile(); $upload->maxSize = 110241024; // 默认为-1,不限制上传大小 $upload->savePath = './upload/'; // 上传根目录 $upload->saveRule = 'uniqid'; // 上传文件的文件名保存规则 $upload->uploadReplace = true; // 如果存在同名文件是否进行覆盖 $upload->autoSub = true; // 上传子目录开启 $upload->subType = 'date'; // 上传子目录命名规则 $upload->allowExts = ['jpg', 'png']; // 允许类型 if ($upload->upload()) { var_dump($upload->getUploadFileInfo()); } else { var_dump($upload->getErrorMsg()); }

  • UpploadFile.html
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>uploadfile test</title>
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <form action="UpploadfileTest.php" method="post" enctype="multipart/form-data">
        <div class="form-group">
            <label>单文件上传</label>
            <input type="file" name="uploadfile">
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>
    <hr>
    <form action="UpploadfileTest.php" method="post" enctype="multipart/form-data">
        <div class="form-group">
            <label>多文件上传</label>
            <input type="file" name="uploadfile[]">
            <input type="file" name="uploadfile[]">
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>
</body>
</html>

4.2.3 本地浏览器访问uploadfile/test/UpploadFile.html进行测试

五、添加 README.mdLICENSE.gitignore等文件,项目最终结构如下:我的包GitHub地址

├─uploadfile                扩展包根目录
│  ├─src                    扩展包代码目录
│  │  ├─UploadFile.php
│  ├─test                   测试目录
│  │  ├─uploadfile.html
│  │  ├─UpploadfileTest.php
│  ├─.gitignore
│  ├─composer.json
│  ├─LICENSE
│  └─README.md

六、推送到 GitHub

git add .
git commit -m 'init'
git tag v1.0.0 // 记住打一个版本号
git push origin master
git push v1.0.0

七、将 GitHub 上的包提交到 Packagist

  1. 首先要在 Packagist 上注册账号并登录(可以用 GitHub 直接登录)
  2. 点击顶部导航条中的 Summit 按钮
  3. 在输入框中输入 GitHub 上的刚才包地址,如:https://github.com/guanguans/uploadfile
  4. 然后点击 Check 按钮 Packagist 会去检测此仓库地址的代码是否符合 Composer 的 Package 包的要求 检测正常的话,会出现 Submit 按钮,再点击一下 Submit 按钮,我们的包就提交到 Packagist 上了

八、设置 composer 包自动更新

上面提交上的包提交的包,当我们更新 GitHub 仓库时,Packagist 上面的的包并不会自动更新,现在我们来设置一下自动更新

8.1 复制 Profile API Token

8.2 打开 GitHub 项目 setting,选择 Integrations & services,添加 packagist service,点击 Test service

8.3 验证是否已经自动更新

移步 Packagist 包主页,发现已经没有了红色的圈住的提示,说明设置自动更新成功。

九、项目中使用

我以 Laravel 中使用举例

composer create-project laravel/laravel
cd laravel
composer require guanguans/uploadfile

其他

(完)

本文为琯琯原创文章,转载无需和我联系,但请注明来自琯琯博客 https://guanguans.cn