2017 © Pedro Peláez
 

library laravel-upload-manager

Upload, validate, storage, manage by API for Laravel 5.1/5.2

image

zgldh/laravel-upload-manager

Upload, validate, storage, manage by API for Laravel 5.1/5.2

  • Monday, April 24, 2017
  • by zgldh
  • Repository
  • 3 Watchers
  • 67 Stars
  • 1,360 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 12 Forks
  • 0 Open issues
  • 9 Versions
  • 8 % Grown

The README.md

laravel-upload-manager

通过API对文件进行“上传、验证、储存、管理”操作。 Upload, validate, storage, manage by API for Laravel 5/6/7/8/9, (*1)

依赖 Requirement

  1. Laravel >= 6.0, (*2)

  2. For Laravel 5, Please use composer require zgldh/laravel-upload-manager 0.6, (*3)

安装 Install

  1. composer require zgldh/laravel-upload-manager
  2. config/app.php 'providers' => [ 'zgldh\UploadManager\UploadManagerServiceProvider']
  3. php artisan vendor:publish --provider="zgldh\UploadManager\UploadManagerServiceProvider"
  4. php artisan migrate
  5. Done

用法 Usage

  1. 上传一个文件 Upload and store a file., (*4)


    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; } }
  2. 从一个URL获取并保存文件 Fetch and store a file from a URL, (*5)


    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; } }
  3. 更新一个上传对象 Update a upload object, (*6)


    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]; } }
  4. 用从一个URL获取到的文件来更新一个上传对象 Update a upload object from a URL, (*7)


    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]; } }
  5. 数据验证 Validation, (*8)


    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); } } }
  6. 存到别的Disk里面, (*9)


    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; } }
  7. 上传前修改$upload 对象, (*10)


    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; } }
  8. 删除某用户上传后,却一直没有使用的avatar类型的$upload 对象, (*11)


    use zgldh\UploadManager\UploadManager; class UploadController extend Controller { public function clearUnUsed(Request $request) { $currentUser = $request->user(); $manager = UploadManager::getInstance(); $manager->removeUnUsedUploads($currentUser, 'avatar'); } }

配置 Configuration

  1. config/upload.php, (*12)

    请查看源文件注释, (*13)

  2. App\Upload, (*14)

    可以在里面写自己喜欢的函数, (*15)

  3. UploadStrategy.php, (*16)

    通常需要你亲自扩展一个出来。如:, (*17)


    <?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 为你自己扩展的类即可。, (*18)

待续, (*19)

The Versions

24/04 2017

dev-master

9999999-dev

Upload, validate, storage, manage by API for Laravel 5.1/5.2

  Sources   Download

MIT

The Requires

 

by Avatar zgldh

laravel api upload manage validate storage

24/04 2017

v0.4.5

0.4.5.0

Upload, validate, storage, manage by API for Laravel 5.1/5.2

  Sources   Download

MIT

The Requires

 

by Avatar zgldh

laravel api upload manage validate storage

13/12 2016

v0.4.4

0.4.4.0

Upload, validate, storage, manage by API for Laravel 5.1/5.2

  Sources   Download

MIT

The Requires

 

by Avatar zgldh

laravel api upload manage validate storage

08/05 2016

v0.4.3

0.4.3.0

Upload, validate, storage, manage by API for Laravel 5.1/5.2

  Sources   Download

MIT

The Requires

 

by Avatar zgldh

laravel api upload manage validate storage

04/03 2016

v0.4.2

0.4.2.0

Upload, validate, storage, manage by API for Laravel 5.1/5.2

  Sources   Download

MIT

The Requires

 

by Avatar zgldh

laravel api upload manage validate storage

18/11 2015

v0.4.1

0.4.1.0

Upload, validate, storage, manage by API for Laravel 5.1

  Sources   Download

MIT

The Requires

 

by Avatar zgldh

laravel api upload manage validate storage

18/11 2015

v0.4.0

0.4.0.0

Upload, validate, storage, manage by API for Laravel 5.1

  Sources   Download

MIT

The Requires

 

by Avatar zgldh

laravel api upload manage validate storage

01/11 2015

v0.3.2

0.3.2.0

Upload, validate, storage, manage by API for Laravel 5.1

  Sources   Download

MIT

The Requires

 

by Avatar zgldh

laravel api upload manage validate storage

27/08 2015

v0.3.1

0.3.1.0

Upload, validate, storage, manage by API for Laravel 5.1

  Sources   Download

MIT

The Requires

 

by Avatar zgldh

laravel api upload manage validate storage