Package Data | |
---|---|
Maintainer Username: | zgldh |
Maintainer Contact: | zgldh@hotmail.com (zgldh) |
Package Create Date: | 2015-07-09 |
Package Last Update: | 2022-02-11 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-15 03:04:28 |
Package Statistics | |
---|---|
Total Downloads: | 5,160 |
Monthly Downloads: | 52 |
Daily Downloads: | 3 |
Total Stars: | 78 |
Total Watchers: | 5 |
Total Forks: | 11 |
Total Open Issues: | 1 |
通过API对文件进行“上传、验证、储存、管理”操作。 Upload, validate, storage, manage by API for Laravel 5.x
config/app.php
'providers' => [ 'zgldh\UploadManager\UploadManagerServiceProvider']
上传一个文件 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');
}
}
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
为你自己扩展的类即可。
待续