Created
March 21, 2020 13:27
-
-
Save martinandersen3d/c084902a532e369864968a356058cdc9 to your computer and use it in GitHub Desktop.
Download File and get FileName methods Laravel Framework
This file contains hidden or 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 Illuminate\Support\Facades\Storage; | |
trait FileTrait | |
{ | |
/** | |
* @param Request $request | |
* @param string $storage | |
* @param null $path | |
* @return string | |
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException | |
*/ | |
public function uploadFile(Request $request, string $storage, $path = null) : string { | |
$hash = md5($request->file). '.' . $request->file('file')->getClientOriginalExtension(); | |
$file = Storage::disk($storage) | |
->put( $path.'/'.$hash, $request->file('file')->get()); | |
if($file) { | |
return $hash; | |
} | |
} | |
/** | |
* @param Request $request | |
* @param string $key | |
* @return string | |
*/ | |
public function getFileName (Request $request, string $key = 'file') : string { | |
return basename($request->file($key)->getClientOriginalName(), '.'.$request->file($key)->getClientOriginalExtension()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment