1
votes

I have a question regarding to public storage and storage itself

After i done inserting the files to the storage. then in the views page i want to display the files that i store to the storage. but i got problem i got error.

Failed to load resource: the server responded with a status of 404 (Not Found)

  • I've run php artisan storage:link
  • The image exists in the public storage folder

Image Exist in the public storage folder & I've run the php artisan storage link

This is my code, how can i insert the file to storage.

$filename =  $request->file->getClientOriginalName();
$request->file->storeAs('storage',$filename); 

This is my code, how to display the image to the view page

@foreach($home_slider as $get_data_slider)
   @if($get_data_slider->slider_sorting == '1')
                
      <a href="{{$get_data_slider->link}}" target="_blank"><img src="{{ asset('/public/storage/'.$get_data_slider->file.'') }}" class="d-block w-100"></a>
                
    @endif
@endforeach

I found out that i can't access the localhost:8000/public/storage/1.jpg

404 not found

But when i use the localhost:8000/storage/1.jpg the file is showing. Showing image when the url is without public

Thanks..

5
try changing the permissions of storage folder to 777?rüff0
how to change permission of storage? @j.albertoureña thanksDevGe
@DevGe instead of asset you should use {{ Storage::url($pr->$get_data_slider->file) }}Salman Zafar
@SalmanZafar yes that storage::url laravel helper working however why this path is not working /public/storage/ and can't viewed to the browserDevGe
it is because you are just linking storage folder to public to make files and other stuff in the storage folder accessible.but in actual the real path of your file is storage/filename..Salman Zafar

5 Answers

6
votes

I had to run php artisan storage:link from the vagrant box to get the symlink to actually take effect.

5
votes

If you use Vagrant, php artisan storage:link should be run under Vagrant. Otherwise, folder mapping would be wrong and that's one of the reasons why do you get 404. So, first run vagrant ssh, then find your project folder and run php artisan storage:link.

1
votes

instead of asset you should use:

{{ Storage::url($pr->$get_data_slider->file) }}

for more info visit file System laravel

0
votes

When you use the helper asset() , you are directly in the public folder. So your src should be

src="{{asset('storage/'.$get_data_slider->file.'')}}".

Everything that is accessible to users from the browser is in the public folder, and for that reason you don't need to put 'public' in the url anymore. That is why the first url (localhost:8000/public/storage/1.jpg) is not found while the second one (localhost:8000/storage/1.jpg) displays your image. When you use the first one, it's like you have another folder named public (which contains the storage folder) into your public folder.

0
votes

asset() - Generate a URL to an application asset.

url() - Generate a URL to an named route.

The asset() method is used to include CSS/JavaScript/images files, you can use it in this cases

<link href="{{ asset('css/min.css') }}" rel="stylesheet">
<script src="{{ asset('use.typekit.net/zjb5wvv.js') }}"></script>
<img alt="logo" src="{{ asset('images/logo.png') }}">

The files must located in the public folder.

In your case :

<a href="{{$get_data_slider->link}}" target="_blank"><img src="{{ asset('storage/'.$get_data_slider->file.'') }}" class="d-block w-100"></a>

P.S : No need to explicitly add public folder inside asset();