siriusphp/upload

框架无关的上传库

4.0.0 2023-11-19 17:38 UTC

This package is auto-updated.

Last update: 2024-09-11 14:26:40 UTC


README

Source Code Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

框架无关的上传处理库。

功能

  1. 验证文件符合常规规则:扩展名、文件大小、图片大小(宽度、高度、比例)。它使用 Sirius Validation 进行此操作。
  2. 将有效的上传文件移动到容器中。容器通常是本地文件夹,但你也可以实现自己的或使用其他文件系统抽象,如 GaufretteFlysystem
  3. 与 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();
}

链接