avatar
save file in folder Laravel

> Handle function to upload object file into storage folder Laravel framework.

$file = $request->file->getClientOriginalName();

$filename = pathinfo($file, PATHINFO_FILENAME);
$extension = pathinfo($file, PATHINFO_EXTENSION);

$path = '/storage/'.strtolower(Str::random(24)).'.'.$extension;
Storage::disk('public')->put($path, file_get_contents($request->file));
\Log::info(URL('/').$path);

> Running php artisan storage:link to create a symlink.

php artisan storage:link

> Create custom Route in web.php as below:

Route::get('/storage/{filename}', function ($filename)
{

    $path = storage_path('app/public/storage/' . $filename);

    if (!File::exists($path)) {
        abort(404);
    }

    $file = File::get($path);
    $type = File::mimeType($path);

    $response = Response::make($file, 200);
    $response->header("Content-Type", $type);

    return $response;
});

> Here is structuring folder in Laravel project where file stored.

> You can access link as http://127.0.0.1:8000/storage/pjzd0hxmxq7d3i01nycjydkl.png

You need to login to do this manipulation!