codeguy/upload 文件上传
最后更新于:2022-04-02 02:23:33
[TOC]
> [github](https://github.com/brandonsavage/Upload)
## 安装
`composer require codeguy/upload`
## 使用
```
$storage = new \Upload\Storage\FileSystem('upload/');//上传目录
$file = new \Upload\File('foo', $storage);
$new_filename = uniqid();
$file->setName($new_filename);
// MimeType List => http://www.iana.org/assignments/media-types/media-types.xhtml
$file->addValidations(array(
//new \Upload\Validation\Mimetype(array('image/png', 'image/gif')),
new \Upload\Validation\Extension(['png','gif','xls']),//不选择不进行过滤
new \Upload\Validation\Size('5M'),//可选 ("B", "K", M", or "G")
));
$data = array(
'name' => $file->getNameWithExtension(),
'extension' => $file->getExtension(),
'mime' => $file->getMimetype(),
'size' => $file->getSize(),
'md5' => $file->getMd5(),
'dimensions' => $file->getDimensions()
);
try {
$file->upload();
print_r($data);
// Array ( [name] => 5da961cdbffdb.png
// [extension] => png
// [mime] => image/png
// [size] => 87278
// [md5] => 4bd0f462764602d5f886a1ed5412743e
// [dimensions] => Array ( [width] => 1191 [height] => 795 ) )
} catch (\Exception $e) {
$errors = $file->getErrors();
var_dump($errors);
}
```
';