siriusphp / upload
框架无关的上传库
4.0.0
2023-11-19 17:38 UTC
Requires
- php: >=8.1
- siriusphp/validation: ^4.0
Requires (Dev)
- laminas/laminas-diactoros: ^3.3
- pestphp/pest: ^2.24
- pestphp/pest-plugin-drift: ^2.5
- phpstan/phpstan: ^1.10
- symfony/http-foundation: ^6.3
- symfony/mime: ^6.3
Suggests
- knplabs/gaufrette: Alternative filesystem abstraction library for upload destinations
- league/flysystem: To upload to different destinations, not just to the local file system
README
框架无关的上传处理库。
功能
- 验证文件符合常规规则:扩展名、文件大小、图片大小(宽度、高度、比例)。它使用 Sirius Validation 进行此操作。
- 将有效的上传文件移动到容器中。容器通常是本地文件夹,但你也可以实现自己的或使用其他文件系统抽象,如 Gaufrette 或 Flysystem。
- 与 PSR7
UploadedFileInterface
对象以及与 Symfony 的UploadedFile
一起工作(参见 集成)。
由 Bolt CMS 使用
简介
use Sirius\Upload\Handler as UploadHandler; $uploadHandler = new UploadHandler('/path/to/local_folder'); // validation rules $uploadHandler->addRule('extension', ['allowed' => ['jpg', 'jpeg', 'png']], '{label} should be a valid image (jpg, jpeg, png)', 'Profile picture'); $uploadHandler->addRule('size', ['max' => '20M'], '{label} should have less than {max}', 'Profile picture'); $result = $uploadHandler->process($_FILES['picture']); // ex: subdirectory/my_headshot.png if ($result->isValid()) { // do something with the image like attaching it to a model etc try { $profile->picture = $result->name; $profile->save(); $result->confirm(); // this will remove the .lock file } catch (\Exception $e) { // something wrong happened, we don't need the uploaded files anymore $result->clear(); throw $e; } } else { // image was not moved to the container, where are error messages $messages = $result->getMessages(); }