Skip to content

Instantly share code, notes, and snippets.

@martinandersen3d
Created March 21, 2020 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinandersen3d/c084902a532e369864968a356058cdc9 to your computer and use it in GitHub Desktop.
Save martinandersen3d/c084902a532e369864968a356058cdc9 to your computer and use it in GitHub Desktop.
Download File and get FileName methods Laravel Framework
<?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