\hymie\uploadFileUpload

文件上传类,支持多文件上传

可选配置参数为:

sub_path:       相对上传目录的子目录,默认 /

allowed_types: 支持的文件类型,会与全局配置合并,若未配置则使用全局配置。'*' 代表支持全部文件类型。

$conf = array('sub_path' => '/a/b/c', 'allowed_types' => 'jpg|png'); $upload = new \hymie\fileupload\FileUpload($conf);

// $result 是一个数组,包含每个文件上传的结果。 $result = $upload->doUpload('file_attribute');

foreach ($result as $k=>$v) { // $k 是原始文件名,如果有重名则会在文件名前加序号,如: 1_filename.ext 。 if ($v['result'] == true) { //do someing; } else { //do someing; } }

如果上传成功,$result 元素子数组包含:

1. result:          上传结果 boolean
2. origFilename:    原始文件名
  1. filemame: 新文件名
  2. fileSize: 文件大小 kb
  3. path: 文件相对上传主目录的路径

如果失败则:

1. result:          上传结果 boolean
  1. msg: 错误信息

Summary

Methods
Properties
Constants
doUpload()
getUpload()
__construct()
getExtension()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
initUploadPath()
initAllowedTypes()
validatePath()
uploadInternal()
error()
success()
isAllowedFiletype()
isAllowedMime()
$allowedTypes
$uploadPath
$subPath
$maxFilename
$config
$mimes
N/A

Properties

$allowedTypes

$allowedTypes : array

支持的文件类型

Type

array

$uploadPath

$uploadPath : string

上传目录,初始为 全局主目录,执行中会赋值为

$uploadPath = $uploadPath . $subPath

Type

string

$subPath

$subPath : string

上传子目录,相对于上传目录

Type

string

$maxFilename

$maxFilename : integer

文件名最大长度,超过这个长度的文件名将无法上传

Type

integer

$config

$config : array

全局配置,参考 config.php 中 upload 相关配置。

Type

array

$mimes

$mimes : array

mime 类型数组,放这里是为了避免重复加载

Type

array

Methods

doUpload()

doUpload(string  $field = 'userfile') : boolean

Perform the file upload

Parameters

string $field

html 文件上传组件 name

Returns

boolean

getUpload()

getUpload(string  $type = 'file', array  $config = array()) : void

获取上传类的工厂方法

Parameters

string $type

上传类型 file | qiniu

array $config

配置数组

__construct()

__construct(  $conf = array()) : void

构造函数

sub_path: 相对上传目录的子目录,默认 / allowed_types: 支持的文件类型,会与全局配置合并,若未配置则使用全局配置。'*' 代表支持全部文件类型。

Parameters

$conf

getExtension()

getExtension(string  $filename) : string

获取文件名后缀

Parameters

string $filename

Returns

string

initUploadPath()

initUploadPath(  $conf) 

初始化上传路径

Parameters

$conf

initAllowedTypes()

initAllowedTypes(string  $globalAllowedTypes, array  $conf) : void

初始化支持的类型,支持的文件类型会合并全局配置与传入的配置

Parameters

string $globalAllowedTypes

全局配置

array $conf

构造函数传递的局部配置

validatePath()

validatePath(  $path) 

Validate Upload Path

Verifies that it is a valid upload path with proper permissions.

Parameters

$path

uploadInternal()

uploadInternal(array  $file) : array

执行具体上传操作

Parameters

array $file

文件信息数组,结构同单文件上传时的 $_FILES

Returns

array —

上传的结果

error()

error(string  $msg) : mixed

上传错误

Parameters

string $msg

错误信息

Returns

mixed

success()

success(string  $origFilename, string  $filename, integer  $fileSize) : array

上传成功

Parameters

string $origFilename

原始文件名

string $filename

新文件名

integer $fileSize

文件大小

Returns

array

isAllowedFiletype()

isAllowedFiletype(  $ext) : boolean

检查文件后缀是否允许上传

Parameters

$ext

Returns

boolean

isAllowedMime()

isAllowedMime(string  $tmpFile, string  $ext) : boolean

检查文件 MIME 是否允许上传,使用 php finfo 库

Parameters

string $tmpFile

临时文件

string $ext

扩展名

Returns

boolean