skip to Main Content

Here is the problem: I would like to display images located in the app/public folder in the browser.
The algorithm is as such:
1-Get the reference;
2-Look for every file having that name in app/public and its subfolders;
3-Display every of them in the blade file.

Here is inside my blade file:

<div class="py-16">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            
                @foreach($matchingFiles as $file)
                    <!--<img src="{{asset('storage/'.$file->getPathname()) }}" alt="{{$file->getFilename() }}">-->
                    <img src="{{ asset('dossiers/' . urlencode($file->getPathname())) }}" alt="{{ $file->getFilename() }}">
                @endforeach`

Here is what is inside my controller:

public function consult($id){
        $tableData=alignement::find($id);
        $reference=$tableData->reference;
        $directory=storage_path("app/public/dossiers");
        $contents=File::allFiles($directory);
        $matchingFiles=[];

        foreach($contents as $file){
            if(strpos($file->getFilename(),$reference) !==false){
                $matchingFiles[]=$file;
            }
        }
        return view('displayFolder',['matchingFiles'=>$matchingFiles]);
    }

The results: It actually detects the images but only returns broken images icons for every file detected. What should I do?

At first, the files were in app/dossiers, but I moved them to app/public/dossiers in the hope that it does display correctly.

2

Answers


  1. Can you change :
    $directory=storage_path("app/public/dossiers");

    By :

    $directory=public_path("app/public/dossiers");

    To try ?

    Login or Signup to reply.
  2. First, you need to make sure that your app/public is accessible from web by running php artisan storage:link in cmd. Make sure that the shortcut folder of storage is created in your public folder.

    Then, you can do as @kris said in the comment like below.

    <img src="{{ asset('storage/dossiers/' . urlencode($file->getPathname())) }}" alt="{{ $file->getFilename() }}">
    
    

    Make sure the path is like storage/app/public/dossiers/image.jpg.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search