Created
May 20, 2020 03:32
-
-
Save mabuak/8b900c981a2d55cb020fcf4e5ea5bad5 to your computer and use it in GitHub Desktop.
File Upload Trait
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created By Fachruzi Ramadhan | |
* | |
* @Filename FileUploadTrait.php | |
* @LastModified 7/9/18 10:11 PM. | |
* | |
* Copyright (c) 2018. All rights reserved. | |
*/ | |
namespace App\Traits; | |
use Illuminate\Support\Facades\Storage; | |
use Illuminate\Support\Facades\Validator; | |
trait fileUploadTrait | |
{ | |
/** | |
* @var string | |
*/ | |
protected $uploadPath = 'uploads'; | |
/** | |
* @var | |
*/ | |
public $folderName; | |
/** | |
* @var string | |
*/ | |
// public $rule = 'image|max:2000'; | |
public $rule = 'image|max:20000'; | |
/** | |
* @var string | |
*/ | |
public $deleteUrl = ''; | |
/** | |
* @return bool | |
*/ | |
private function createUploadFolder(): bool | |
{ | |
if (!file_exists(config('filesystems.disks.public.root') . '/' . $this->uploadPath . '/' . $this->folderName)) { | |
$attachmentPath = config('filesystems.disks.public.root') . '/' . $this->uploadPath . '/' . $this->folderName; | |
mkdir($attachmentPath, 0755, true); | |
Storage::disk('s3')->put('public/' . $this->uploadPath . '/' . $this->folderName . '/index.html', 'Silent Is Golden'); | |
return true; | |
} | |
return false; | |
} | |
/** | |
* For handle validation file action | |
* | |
* @param $file | |
* @return fileUploadTrait|\Illuminate\Http\RedirectResponse | |
*/ | |
private function validateFileAction($file) | |
{ | |
$rules = array('fileupload' => $this->rule); | |
$file = array('fileupload' => $file); | |
$fileValidator = Validator::make($file, $rules); | |
if ($fileValidator->fails()) { | |
$messages = $fileValidator->messages(); | |
return redirect()->back()->withInput(request()->all()) | |
->withErrors($messages); | |
} | |
} | |
/** | |
* For Handle validation file | |
* | |
* @param $files | |
* @return fileUploadTrait|\Illuminate\Http\RedirectResponse | |
*/ | |
private function validateFile($files) | |
{ | |
if (is_array($files)) { | |
foreach ($files as $file) { | |
return $this->validateFileAction($file); | |
} | |
} | |
return $this->validateFileAction($files); | |
} | |
/** | |
* For Handle Put File | |
* | |
* @param $file | |
* @return bool|string | |
*/ | |
private function putFile($file) | |
{ | |
$fileName = preg_replace('/\s+/', '_', time() . ' ' . $file->getClientOriginalName()); | |
$path = $this->uploadPath . '/' . $this->folderName; | |
if (Storage::disk('s3')->putFileAs('public/' . $path, $file, $fileName)) { | |
return $path . $fileName; | |
} | |
return false; | |
} | |
/** | |
* For Handle Save File Process | |
* | |
* @param $files | |
* @return array | |
*/ | |
public function saveFiles($files) | |
{ | |
$data = null; | |
if($files != null){ | |
$this->validateFile($files); | |
$this->createUploadFolder(); | |
if (is_array($files)) { | |
foreach ($files as $file) { | |
if(is_object($file)){ | |
$data[] = $this->putFile($file); | |
} | |
} | |
} else { | |
$data = $this->putFile($files); | |
} | |
} | |
return $data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment