cohensive / upload
处理文件上传。
v2.5.2
2017-08-18 16:11 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- mockery/mockery: dev-master
- phpunit/phpunit: ~4.0
README
通过标准的HTML多部分表单或通过XHR数据流处理文件上传。
使用方法
Upload可以作为任何类单独使用,也可以作为Laravel 4框架的包使用(如果您想添加对其他框架的支持,请在问题中留言或提交包含所需更改的pull request)。
首先,将以下行添加到composer.json中需求列表中
"cohensive/upload": "dev-master"
运行composer install
或composer update
来下载并自动加载。
Laravel 4/5
获取包配置文件 - 不强制要求,但如果有许多不同的上传字段并且希望更改默认的Upload选项,可能会很有用。
// Laravel 4 php artisan config:publish cohensive/upload // Laravel 5 php artisan vendor:publish
在providers
数组中添加新的包
'providers' => array( //... 'Cohensive\Upload\UploadServiceProvider', //... )
在别名字段添加Upload外观
'aliases' => array( //... 'Upload' => 'Cohensive\Upload\Facades\Laravel\Upload' //... )
在代码中使用
// Create new Upload instance. // Both params are optional arrays. Description of allowed array items below. $upload = Upload::make($rules, $options); if ($upload->passes()) { // If file is valid, save it in the uploads (set in options) directory. $file = $upload->save(); // $file - is an instance of Cohensive\Upload\File class with various file-related attributes and methods. } else { // Get array of errors - simple list of failed validation rules. $upload->getErrors(); }
上传文件后的处理超出了Upload的范围。
独立使用
作为独立包使用时,您需要实例化Upload所依赖的几个类。具体包括
-
验证器 - 将对文件进行一系列规则的验证。
-
消毒剂 - 将确保文件名对Web安全(转换为ascii)。它需要一个必需的参数 - 实例化的类,该类将执行文件消毒。不同的框架有自己的版本,通常称为转换器或屈折变化器。您只需将其实例作为第一个参数使用,并将其作为第二个参数包含用于消毒的方法名。或者,您可以创建一个用于您的屈折变化器的包装器,该包装器实现了\ Censive\ Upload\ Sanitizer\ SanitizerInterface
-
文件处理器工厂 - 将根据上传方法创建文件处理器
$validator = new \Cohensive\Upload\Validator([ 'maxSize' => 2097152 // Set maximum allowed filesize to 2MB (set in bytes) ]); $sanitizer = new \Cohensive\Upload\Sanitizer\AbstractSanitizer($sanitizerClass, $sanitizerMethod); $fileFactory = new \Cohensive\Upload\FileHandlerFactory(); $options = [...]; // An array of options for Upload, such as upload directory etc. $upload = new \Cohensive\Upload\Upload($validator, $sanitizer, $fileFactory, $options);
在代码中使用
$rules = [...]; // An array of validation rules. Optional. if ($upload->passes($rules)) { // If file is valid, save it in the uploads (set in options) directory. $file = $upload->save(); } else { // Get array of errors - simple list of failed validation rules. $errors = $validator->getErrors(); }
处理文件后保存。
文件成功验证和保存后,Upload将返回包含多个方法的File
类实例
$file->move($destination); // Move file to another directory. Returns boolean. $file->delete(); // Delete file. $file->getFileinfo(); // Returns array containing file information. $file->getFilepath(); // Returns path to the file. $file->getMimetype(); // Returns file MIME. $file->getExtension(); // Returns file extension. $file->isExists(); // Checks if files exists. Returns boolean.
选项
上传器在实例化时接受一个选项数组
'uploadDir' => 'uploads/', // Folder where all uploaded files will be saved to. 'tmpDir' => 'uploads/tmp/', // Folder to keep files temporary for operations. 'param' => 'file', // Parameter to access the file on. 'name' => '', // Set new filename. Blank to use original name. 'nameLength' => 32, // Set maximum length of the name. Will be cut if longer. 'prefix' => '', // Add prefix to the filename.. 'suffix' => '', // Add suffix to the filename. 'case' => '', // Convert file name to the case: 'lower', 'upper' or falsy to keep original. 'overwrite' => false, // If file already exists, overwrite it. 'autoRename' => true, // In case if file with the same name exists append counter to the new file. 'randomize' => false, // Generate random filename. Boolean or integer for string length. Default length is 10. 'sanitize' => true // Sanitize filename - remove whitespaces and convert utf8 to ascii.
规则
一个规则数组,可以在验证上传文件时使用
'minSize' => 0, // Minimum filesize. 'maxSize' => 10485760, // Maximum filesize: 10MB. 'maxWidth' => 0, // Maximum image width if file is an image. 'maxHeight' => 0, // Maximum image height if file is an image. 'minWidth' => 0, // Minimum image width if file is an image. 'minHeight' => 0, // Minimum image height if file is an image. 'width' => [], // Image must have exact width (use array to set multiple). 'height' => [], // Image must have exact height (use array to set multiple). 'whiteExt' => ['jpg', 'jpeg', 'gif', 'png'], // Array of allowed extensions. 'blackExt' => [] // Array of disallowed extensions.