shoperti / uploader
Laravel 的智能文件上传器
v5.8.0
2020-12-07 16:54 UTC
Requires
- php: ^7.2
- illuminate/contracts: 5.7.* || 5.8.*
- illuminate/filesystem: 5.7.* || 5.8.*
- illuminate/http: 5.7.* || 5.8.*
- illuminate/support: 5.7.* || 5.8.*
- intervention/image: ^2.5
Requires (Dev)
- graham-campbell/testbench: ^5.5
- phpunit/phpunit: ^7.5
README
支持使用 League/Glide 预处理图片(调整大小和自动旋转)的 Laravel 文件上传器。
注意:为了自动旋转,您必须有一个支持读取 EXIF 的 PHP 安装。
安装
添加到 composer
"require": { "shoperti/uploader": "~5.8" },
在您的提供者中注册包
public function register() { $this->app->register(\Shoperti\Uploader\UploaderServiceProvider::class); }
配置
您可以通过 .env 文件使用以下设置进行配置
UPLOADER_FILES_DISK=s3
UPLOADER_FILES_SUBPATH=files
UPLOADER_FILES_FILE_NAMING=fix
UPLOADER_IMAGES_DISK=s3
UPLOADER_IMAGES_SUBPATH=images
UPLOADER_IMAGES_FILE_NAMING=fix
UPLOADER_IMAGES_RESIZE_MAX_WIDTH=1280
UPLOADER_IMAGES_RESIZE_MEMORY_LIMIT=128M
- 有关更多详细信息,请参阅配置文件。
使用
<?php namespace App\Http\Controllers; // Import the Shoperti Uploader Manager in your controller use Shoperti\Uploader\Contracts\UploaderManager; use Shoperti\Uploader\Exceptions\DisallowedFileException; use Shoperti\Uploader\Exceptions\InvalidFileException; use Shoperti\Uploader\Exceptions\RemoteFileException; // To upload or delete a file, just inject the manager either in the constructor // or in the action method class Controller extends BaseController { /** * Uploads a file. * * @param \Shoperti\Uploader\Contracts\UploaderManager $uploaderManager */ public function upload(UploaderManager $uploaderManager) { try { /** @var \Shoperti\Uploader\UploadResult uploadResult */ $uploadResult = $uploaderManager // generate an Uploader through the manager // using the uploaded file or the file URL as argument ->make(request()->file('file') ?: request()->input('file')) // then call the upload() method with the location path as argument ->upload($path = 'my_files', $disk = null); } catch (DisallowedFileException $dfe) { // If the uploaded file has a disallowed mime-type } catch (InvalidFileException $ife) { // If the uploaded file is invalid } catch (RemoteFileException $rfe) { // If the file input was a file-url string which cannot be fetched } } /** * Deletes a file. * * @param \Shoperti\Uploader\Contracts\UploaderManager $uploaderManager */ public function delete(UploaderManager $uploaderManager) { $uploaderManager ->delete($disk = 's3', $filepath = \Request::input('file')) } }
- 有关更多详细信息,请参阅
UploadResult
实现。