Created
May 19, 2018 09:27
-
-
Save mabuak/6bd06a49e4b98ea63b781ecdbd711f40 to your computer and use it in GitHub Desktop.
Laravel 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 PhpStorm. | |
* User: DhanPris | |
* Date: 15/05/2018 | |
* Time: 11:51 | |
*/ | |
namespace App\Http\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'; | |
/** | |
* @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, 0777); | |
Storage::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::putFileAs('public/' . $path, $file, $fileName)) { | |
return $path . $fileName; | |
} | |
return false; | |
} | |
/** | |
* For Handle Save File Process | |
* | |
* @param $files | |
* @return array | |
*/ | |
public function saveFiles($files) | |
{ | |
$data = []; | |
if($files != null){ | |
$this->validateFile($files); | |
$this->createUploadFolder(); | |
if (is_array($files)) { | |
foreach ($files as $file) { | |
$data[] = $this->putFile($file); | |
} | |
} else { | |
$data[] = $this->putFile($files); | |
} | |
} | |
return $data; | |
} | |
} |
Okay i did a search and i find out we have to do this way.. I dunno it's clean but it's working :)
https://laracasts.com/discuss/channels/laravel/how-to-redirect-with-messege-inside-trait
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's really helpful but there is something i think does not work with validation and it keep accepting all file types as an image to upload.
it's like RedirectResponse not working in trait..