Created
October 6, 2017 06:28
-
-
Save brickgale/3d1c2ce4bc49c4f6f1e21db118e90b8d to your computer and use it in GitHub Desktop.
File Trait for Laravel 5.3 below
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 | |
namespace App\Traits; | |
use Illuminate\Http\Request; | |
use File; | |
trait FileTrait { | |
/** | |
* Upload User Avatar | |
* | |
* @param Request | |
* @return path | |
*/ | |
public function uploadFile(Request $request, $field_key) | |
{ | |
$file_key = $request->file_key; | |
$a_file = []; | |
if ($request->hasFile($file_key)) | |
{ | |
$file_prefix = $request->has('file_prefix') ? $request->input('file_prefix') : 'file'; | |
$file_path = $request->has('file_path') ? $request->input('file_path') : 'uploads/'; | |
$file = $request->file($file_key); | |
$a_file['file_name'] = $file_prefix.time().'-'.rand(111,999).'.'.$file->getClientOriginalExtension(); | |
//removed original file name temporarily due to saving filename character bug | |
// $a_file['file_name'] = $file_prefix.time().'-'.rand(111,999).$file->getClientOriginalName(); | |
$a_file['mime_type'] = $file->getClientMimeType(); | |
$a_file['size'] = $file->getClientSize(); | |
$a_file[$field_key] = $file_path.'/'.$a_file['file_name']; | |
$this->storage->put( | |
$a_file[$field_key], | |
File::get($file->getrealpath()) | |
); | |
if($request->has('current_file_url')) { | |
$this->storage->readAndDelete($request->input('current_file_url')); | |
} | |
} | |
return $a_file; | |
} | |
/** | |
* Show File | |
* | |
* @param Image path | |
* @return image | |
*/ | |
public function showFile($file_path, $file_name) | |
{ | |
$a_file_details = $this->checkFile($file_path, $file_name); | |
if(!is_array($a_file_details)) { | |
return $a_file_details; | |
} | |
// return response()->download($path, $filename); | |
return response()->make(File::get($a_file_details['path']), 200)->header('Content-type', $a_file_details['mime']); | |
} | |
/** | |
* Check File Exist | |
* | |
* @param File path | |
* @return path, file, filename, mime | |
*/ | |
public function checkFile($file_path, $file_name = null) | |
{ | |
$exists = $this->storage->has($file_path); | |
$path = storage_path(sprintf('app/public/%s', $file_path)); | |
$file_info = pathinfo($path); | |
$filename = $file_info['basename']; | |
if (!$exists || (!is_null($file_name) && $file_name != $filename) ) { | |
return response()->make(['message' => 'File not found.'], 404); | |
} | |
$file = $this->storage->get($file_path); | |
$mime = $this->storage->mimeType($file_path); | |
$size = $this->storage->size($file_path); | |
$no_ext = $file_info['filename']; | |
return compact('path','filename','file','mime','size','no_ext'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment