Alaksandar Jesus Gene
Alaksandar Jesus Gene

Reputation: 6883

Laravel Retrieve Images from storage to view

I am using below code to store the uploaded file

 $file = $request->file($file_attachment);
        $rules = [];
        $rules[$file_attachment] = 'required|mimes:jpeg|max:500';
        $validator = Validator::make($request->all(), $rules);
        if ($validator->fails()) {
            return redirect()->back()
                ->with('uploadErrors', $validator->errors());
        }

        $userid = session()->get('user')->id;
        $destinationPath = config('app.filesDestinationPath') . '/' . $userid . '/';
        $uploaded = Storage::put($destinationPath . $file_attachment . '.' . $file->getClientOriginalExtension(), file_get_contents($file->getRealPath()));

The uploaded files are stored in storage/app/2/filename.jpg

I want to show back the user the file he uploaded. How can i do that?

$storage = Storage::get('/2/filename.jpg');

I am getting unreadable texts. I can confirm that the file is read. But how to show it as an image to the user.

Hope i made my point clear.

Working Solution

display.blade.php

<img src="{{ URL::asset('storage/photo.jpg') }}" />

web.php

Route::group(['middleware' => ['web']], function () {
    Route::get('storage/{filename}', function ($filename) {
        $userid = session()->get('user')->id;
        return Storage::get($userid . '/' . $filename);
    });
});

Thanks to: @Boghani Chirag and @rkj

Upvotes: 14

Views: 78157

Answers (8)

Ali Raza
Ali Raza

Reputation: 66

Create Route:

Route::get('image/{filename}', 'HomeController@displayImage')->name('image.displayImage');

Create Controller Method:

public function displayImage($filename)

{

  

    $path = storage_public('images/' . $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;

}

<img src="{{ route('image.displayImage',$article->image_name) }}" alt="" title="">

Upvotes: 1

Micro Jafar
Micro Jafar

Reputation: 61

Laravel 8

Controller

class MediaController extends Controller
{ 

    public function show(Request $request, $filename)
    {
        $folder_name = 'upload';

        $filename = 'example_img.jpeg';

        $path = $folder_name.'/'.$filename;

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

        return Storage::response($path);
    }
}

Route

Route::get('media/{filename}', [\App\Http\Controllers\MediaController::class, 'show']);

Upvotes: 3

Abdul Manan
Abdul Manan

Reputation: 2375

Uploaded like this

$uploadedFile = $request->file('photo');
$photo = "my-prefix" . "_" . time() . "." . $uploadedFile->getClientOriginalExtension();
$photoPath = \Illuminate\Support\Facades\Storage::disk('local')->putFileAs(
   "public/avatar",
   $uploadedFile,
   $photo
);

and then access like this

<img src="{{ asset('storage/avatar/'.$filename) }}" />

Upvotes: 1

Parth Kharecha
Parth Kharecha

Reputation: 6503

enter image description here

Remember put your folder in storage/app/public/

Create the symbolic linksymbolic link to access this folder

php artisan storage:link

if you want to access profile images of 2 folder then do like this in your blade file

<img src="{{ asset('storage/2/images/'.$user->profile_image) }}" />

Upvotes: 7

Boghani Chirag
Boghani Chirag

Reputation: 225

Can you please try this code

routes.php

Route::group(['middleware' => ['web']], function() {
    Route::get('storage/storage_inner_folder_fullpath/{filename}', function ($filename) {
        return Image::make(storage_path() . '/storage_inner_folder_fullpath/' . $filename)->response();
    });
});

view file code

<img src="{{ URL::asset('storage/storage_inner_folder_fullpath/'.$filename) }}" />

Thanks

Upvotes: 2

rkj
rkj

Reputation: 8287

File not publicly accessible like you said then read file like this

$userid = session()->get('user')->id;
$contents = Storage::get($userid.'/file.jpg'); 

Assuming your file is at path storage/app/{$userid}/file.jpg and default disk is local check config/filesystems.php

File publicly accessible

If you want to make your file publicly accessible then store file inside this storage/app/public folder. You can create subfolders inside it and upload there. Once you store file inside storage/app/public then you have to just create a symbolic link and laravel has artisan command for it.

php artisan storage:link

This create a symbolic link of storage/app/public to public/storage. Means now you can access your file like this

$contents = Storage::disk('public')->get('file.jpg'); 

here the file physical path is at storage/app/public/file.jpg and it access through symbolic link path public/storage/file.jpg

Suppose you have subfolder storage/app/public/uploads where you store your uploaded files then you can access it like this

$contents = Storage::disk('public')->get('uploads/file.jpg');

When you make your upload in public folder then you can access it in view

echo asset('storage/file.jpg'); //without subfolder uploads

echo asset('storage/uploads/file.jpg');

check for details https://laravel.com/docs/5.6/filesystem#configuration

Upvotes: 21

Gautam Patadiya
Gautam Patadiya

Reputation: 1411

Try this:

Storage::disk('your_disk_name')->getDriver()->getAdapter()->applyPathPrefix('your_file_name');

Good Luck !

Upvotes: 1

Rohit Jadhav
Rohit Jadhav

Reputation: 82

can You please try this

$storage = Storage::get(['type_column_name']);

Upvotes: 0

Related Questions