zgldh / laravel-upload-manager
Upload, validate, storage, manage by API for Laravel 5/6/7/8/9
Installs: 5 188
Dependents: 2
Suggesters: 0
Security: 0
Stars: 78
Watchers: 5
Forks: 11
Open Issues: 1
Requires
- laravel/framework: ^6.0|^7.0|^8.0|^9.0
README
通过API对文件进行“上传、验证、储存、管理”操作。 Upload, validate, storage, manage by API for Laravel 5/6/7/8/9
依赖 Requirement
-
Laravel >= 6.0
-
For Laravel 5, Please use
composer require zgldh/laravel-upload-manager 0.6
安装 Install
- composer require zgldh/laravel-upload-manager
config/app.php
'providers' => [ 'zgldh\UploadManager\UploadManagerServiceProvider']
- php artisan vendor:publish --provider="zgldh\UploadManager\UploadManagerServiceProvider"
- php artisan migrate
- Done
用法 Usage
-
上传一个文件 Upload and store a file.
use zgldh\UploadManager\UploadManager; class UploadController extend Controller { public function postUpload(Request $request) { $file = $request->file('avatar'); $manager = UploadManager::getInstance(); $upload = $manager->upload($file); $upload->save(); return $upload; } }
-
从一个URL获取并保存文件 Fetch and store a file from a URL
use zgldh\UploadManager\UploadManager; class UploadController extend Controller { public function postUpload(Request $request) { $fileUrl = $request->input('url'); $manager = UploadManager::getInstance(); $upload = $manager->upload($fileUrl); $upload->save(); return $upload; } }
-
更新一个上传对象 Update a upload object
use App\Upload; use zgldh\UploadManager\UploadManager; class UploadController extend Controller { public function postUpload(Request $request) { $uploadId = $request->input('id'); $file = $request->file('avatar'); $manager = UploadManager::getInstance(); $upload = Upload::find($uploadId); if($manager->update($upload, $file)) { $upload->save(); return $upload; } return ['result'=>false]; } }
-
用从一个URL获取到的文件来更新一个上传对象 Update a upload object from a URL
use App\Upload; use zgldh\UploadManager\UploadManager; class UploadController extend Controller { public function postUpload(Request $request) { $uploadId = $request->input('id'); $fileUrl = $request->input('url'); $manager = UploadManager::getInstance(); $upload = Upload::find($uploadId); if($manager->update($upload, $fileUrl)) { $upload->save(); return $upload; } return ['result'=>false]; } }
-
数据验证 Validation
use zgldh\UploadManager\UploadManager; class UploadController extend Controller { public function postUpload(Request $request) { $file = $request->file('avatar'); $manager = UploadManager::getInstance(); $upload = $manager->withValidator('image')->upload($file); //加上验证组 if($upload) { $upload->save(); return $upload; } else { $errorMessages = $manager->getErrors(); //得到所有错误信息 $errorMessage = $manager->getFirstErrorMessage(); //得到第一条错误信息 throw new \Exception($errorMessage); } } }
-
存到别的Disk里面
use zgldh\UploadManager\UploadManager; class UploadController extend Controller { public function postUpload(Request $request) { $file = $request->file('avatar'); $manager = UploadManager::getInstance(); $upload = $manager ->withValidator('image') ->withDisk('qiniu') // 储存到七牛磁盘里 ->upload($file); $upload->save(); return $upload; } }
-
上传前修改
$upload
对象use zgldh\UploadManager\UploadManager; class UploadController extend Controller { public function postUpload(Request $request) { $file = $request->file('avatar'); $manager = UploadManager::getInstance(); $upload = $manager ->withValidator('image') ->withDisk('localhost') // 默认存到本地 ->upload($file, function($upload){ if($upload->size > 1024*1024) { $upload->disk = 'qiniu';// 超过1兆的文件都放到七牛里。 } return $upload; }); $upload->save(); return $upload; } }
-
删除某用户上传后,却一直没有使用的
avatar
类型的$upload
对象use zgldh\UploadManager\UploadManager; class UploadController extend Controller { public function clearUnUsed(Request $request) { $currentUser = $request->user(); $manager = UploadManager::getInstance(); $manager->removeUnUsedUploads($currentUser, 'avatar'); } }
配置 Configuration
-
config/upload.php
请查看源文件注释
-
App\Upload
可以在里面写自己喜欢的函数
-
UploadStrategy.php
通常需要你亲自扩展一个出来。如:
<?php namespace App\Extensions; use zgldh\UploadManager\UploadStrategy as BaseUploadStrategy; use zgldh\UploadManager\UploadStrategyInterface; class UploadStrategy extends BaseUploadStrategy implements UploadStrategyInterface { /** * 生成储存的相对路径 * @param $filename * @return string */ public function makeStorePath($filename) { $path = 'i/' . $filename; return $path; } /** * 得到 disk localuploads 内上传的文件的URL * @param $path * @return string */ public function getLocaluploadsUrl($path) { $url = url('uploads/' . $path); return $url; } /** * 得到 disk qiniu 内上传的文件的URL * @param $path * @return string */ public function getQiniuUrl($path) { $url = 'http://' . trim(\Config::get('filesystems.disks.qiniu.domain'), '/') . '/' . trim($path, '/'); return $url; } }
然后在
config/upload.php
里面配置upload_strategy
为你自己扩展的类即可。
待续