Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thecelebratedmrk's avatar

How to display all files in a directory in my view

What I'm trying to do:

My Storage/Images-directory has a lot of images that I want to display as a gallery. Something like:

@foreach($files as $file)

img src="{{$file}}"

@endforeach

I've looked at https://laravel.com/docs/7.x/filesystem#directories already, but haven't had any luck so far.

What I don't understand:

  • Which directory should I point to? Do I use asset() in the same way as when I link to my app.css? Do I link directly to www.mydomain.com/storage/images?
  • Where do I add the $files = Storage::allFiles($directory);

I suspect that I should do something in routes/web.php, app/Http/PagesController.php and a custom view, but I don't know where to add what.

Any help appreciated.

0 likes
5 replies
martinbean's avatar

Which directory should I point to? Do I use asset() in the same way as when I link to my app.css? Do I link directly to www.mydomain.com/storage/images?

@thecelebratedmrk You should point to whatever directory you store your files in.

Where do I add the $files = Storage::allFiles($directory);

You could do this in the controller, and then pass the array of files to your Blade view. You would then just do a @​foreach over your array of files.

However, this probably isn’t what you want to do if you’re uploading say, 2MB images. Ideally, you should process images and create appropriately-sized thumbnails of uploaded images if you’re building a photo gallery, and then show a larger-sized images when the user selects a thumbnail.

thecelebratedmrk's avatar

An image gallery is only one of the applications I want to use this script for. To make it a bit easier: Let's say I just want to display a list of PDF-files, and link to them.

Here's what I have so far:

PagesController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class PagesController extends Controller
{
    public function files() {
        $directory =  'storage/pdfs';
        $storage = Storage::allFiles($directory);
        $data = array(
            'title' => 'All files',
            'files' => $storage
        );
        return view('inc.file')->with($data);
    }
}

file.blade.php

<h1 class="title has-text-white">{{ $title }}</h1>
      <div class="columns is-centered">
          @foreach($files as $file)
            <div class="column is-3">
              <div class="box">
                <a href="" class="button is-primary">{{$file}}</a>
              </div>
            </div>
          @endforeach

      </div>

It displays the title, but nothing happens with the foreach-loop. When try to use count() to measure the length of $storage it shows 0, even though the directory contains many files.

Have I forgotten to include or install some package? Is something wrong with my path? I've scratched my head on this for hours now.

chr15k's avatar

You could use the File facade which should give you an array of Symfony\Component\Finder\SplFileInfo instances:

use Illuminate\Support\Facades\File;

$storage = File::allFiles(storage_path('pdfs'));


=> [
     Symfony\Component\Finder\SplFileInfo {#3371
       path: "/project-directory/storage/pdfs",
       filename: "test.pdf",
       basename: "test.pdf",
       pathname: "/project-directory/storage/pdfs/test.pdf",
       extension: "pdf",
       realPath: "/project-directory/storage/pdfs/test.pdf",
       aTime: 2020-07-27 23:03:25,
       mTime: 2020-07-27 23:03:25,
       cTime: 2020-07-27 23:03:25,
       inode: 13314623,
       size: 0,
       perms: 0100644,
       owner: 501,
       group: 20,
       type: "file",
       writable: true,
       readable: true,
       executable: false,
       file: true,
       dir: false,
       link: false,
     },
   ]

Example to get filename:

use Illuminate\Support\Facades\File;

$storage = File::allFiles(storage_path('pdfs'));

foreach ($storage as $file) {
    echo $file->getFilename();
}

// test.pdf

If these files need to be publicly accessible then you can create a sym link from storage to public directory, then use the asset helper:

https://laravel.com/docs/7.x/filesystem#the-public-disk

1 like

Please or to participate in this conversation.