简体   繁体   中英

How to retrieve the images stored in storage folder of laravel5.4?

Storage/app/public -->This is my storage path of an images.

Controller code:

public function addDeviceCategory(Request $cform)
{

if (Input::hasFile('imagefile'))
    {
    $name  = $this->getImageId().".".$cform->file('imagefile')-
 >getClientOriginalExtension();   

    $image = $cform->file('imagefile');        
    $resize = Image::make($image)->resize(700, 300)->encode($cform-
 >file('imagefile')->getClientOriginalExtension());
    $hash = md5($resize->__toString());

    $path = "public/" . $name;
    Storage::put($path, $resize->__toString());

    }
  $validation1=deviceCategory::select('deviceCategoryName')-
 >where("deviceCategoryName",$cform->deviceCategory)->first();

    if(is_null($validation1))
    {

    $temp=new deviceCategory;

    $temp->deviceCategoryId=$this->getDeviceCategoryId();
    $temp->deviceCategoryName=$cform->input('deviceCategory');
    $temp->subCategoryId=$cform->input('subCategory');
    $temp->image=$name;

    $temp->save();

    return redirect('formAddDeviceCategory');
   }

}

public function getImageId(){
  return md5(uniqid().microtime());
}

This is the way I'm storing the images into storage path of laravel(ie. Storage/app/public).The storage done successfully here.

View Code:

 @php 
            $l=1
            @endphp
            @foreach($cdetails as $cdetail)
            <tr>
              <td>{{$l++}}</td>
              <td>{{$cdetail->deviceCategoryId}}</td>
              <td>{{$cdetail->categoryName}}</td>
              <td>{{$cdetail->subCategoryName}}</td>
              <td>{{$cdetail->deviceCategoryName}}</td>

          **<td><img src="{{ asset('storage/app/public/$cdetail->
              image') }}"  width="50" height="50"></img></td>**
             @endforeach

Here I am trying to display the stored image.But nothing displays there.

Use asset() function like

example in your blade template: {{ asset('storage/app/public/'.$cdetail->image ) }}

In case if you want to access in your controller asset('storage/app/public/'.$cdetail->image );

<td>
   <img src="{{ asset('storage/app/public/'.$cdetail->image) }}" width="50" 
    height="50"></img>
</td>

First make sure you have linked the storage/app/public to public/storage .

If not run php artisan storage:link

Then in your view use Storage::url('public/' . $cdetail->image) to get the link.

I think you can try this:

<img src="{{ storage_path('app/public/$cdetail->
              image') }}"  width="50" height="50"></img>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM