PHP code example of reg2005 / laravel-upload-manager
1. Go to this page and download the library: Download reg2005/laravel-upload-manager library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
reg2005 / laravel-upload-manager example snippets
use reg2005\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;
}
}
use reg2005\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;
}
}
use App\Upload;
use reg2005\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];
}
}
use App\Upload;
use reg2005\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];
}
}
use reg2005\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);
}
}
}
use reg2005\UploadManager\UploadManager;
class UploadController extend Controller
{
public function postUpload(Request $request)
{
$file = $request->file('avatar');
$manager = UploadManager::getInstance();
$upload = $manager
->withValidator('image')
->withDisk('selectel') // 储存到七牛磁盘里
->upload($file);
$upload->save();
return $upload;
}
}