Skip to content

Instantly share code, notes, and snippets.

@mccabiles
Last active August 15, 2019 17:56
Show Gist options
  • Save mccabiles/df7ddb34c09a31664d6ac423839a4299 to your computer and use it in GitHub Desktop.
Save mccabiles/df7ddb34c09a31664d6ac423839a4299 to your computer and use it in GitHub Desktop.
Base64 Image uploading for Laravel 5.6+ using Storage Facade
use Illuminate\Support\Facades\Storage;
//...
$base64image = '...';
@list($type, $file_data) = explode(';', $base64image);
@list(, $file_data) = explode(',', $file_data);
$type = explode(";", explode("/", $base64image)[1])[0];
$path = 'images/' . time() . '.' . $type;
Storage::disk('local')->put($path, base64_decode($file_data));
Route::get('images/{file}', function($file)
{
$type = pathinfo($file); // get file type
$file = Storage::disk(env('STORAGE_DRIVER', 'local'))->get('images/'.$file); // retrieve the file from storage
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment