How to obtain a list of all files in a public folder in Laravel?


To get the list of all files from public folder you can make use of File facade. To work with it you need to include following class

use Illuminate\Support\Facades\File;

Following files are present inside public folder −

Example 1

To get all files from public folder

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\File; class UserController extends Controller{ public function index() { $path = public_path(); $files = File::allFiles($path); print_r($files); } }

Output

The output of the above code is −

Array(
   [0] => Symfony\Component\Finder\SplFileInfo Object(
      [relativePath:Symfony\Component\Finder\SplFileInfo:private] => css
      [relativePathname:Symfony\Component\Finder\SplFileInfo:private] => css\test.css
      [pathName:SplFileInfo:private] => C:\xampp\htdocs\laraveltest\public\css\test.css
      [fileName:SplFileInfo:private] => test.css
   )
   
   [1] => Symfony\Component\Finder\SplFileInfo Object(
      [relativePath:Symfony\Component\Finder\SplFileInfo:private] =>
      [relativePathname:Symfony\Component\Finder\SplFileInfo:private] => favicon.ico
      [pathName:SplFileInfo:private] => C:\xampp\htdocs\laraveltest\public\favicon.ico
      [fileName:SplFileInfo:private] => favicon.ico
   )

   [2] => Symfony\Component\Finder\SplFileInfo Object(
      [relativePath:Symfony\Component\Finder\SplFileInfo:private] => images
      [relativePathname:Symfony\Component\Finder\SplFileInfo:private] => images\flower.jpg
      [pathName:SplFileInfo:private] => C:\xampp\htdocs\laraveltest\public\images\flower.jpg
      [fileName:SplFileInfo:private] => flower.jpg
   )
   
   [3] => Symfony\Component\Finder\SplFileInfo Object(
      [relativePath:Symfony\Component\Finder\SplFileInfo:private] =>
      [relativePathname:Symfony\Component\Finder\SplFileInfo:private] => index.php
      [pathName:SplFileInfo:private] => C:\xampp\htdocs\laraveltest\public\index.php
      [fileName:SplFileInfo:private] => index.php
   )

   [4] => Symfony\Component\Finder\SplFileInfo Object(
      [relativePath:Symfony\Component\Finder\SplFileInfo:private] =>
      [relativePathname:Symfony\Component\Finder\SplFileInfo:private] => robots.txt
      [pathName:SplFileInfo:private] => C:\xampp\htdocs\laraveltest\public\robots.txt
      [fileName:SplFileInfo:private] => robots.txt
   )
)

Example 2

To get all the images from public folder.

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use Illuminate\Support\Facades\Storage; class UserController extends Controller{ public function index() { $images = \File::allFiles(public_path('images')); print_r($images); } }

Output

The output for the above is as follows.

Array(
   [0] => Symfony\Component\Finder\SplFileInfo Object(
      [relativePath:Symfony\Component\Finder\SplFileInfo:private] =>
      [relativePathname:Symfony\Component\Finder\SplFileInfo:private] => flower.jpg
      [pathName:SplFileInfo:private] => C:\xampp\htdocs\laraveltest\public\images\flower.jpg
      [fileName:SplFileInfo:private] => flower.jpg
   )
)

Example 3

To get all CSS files from public folder −

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller{ public function index() { $css_files = \File::allFiles(public_path('css')); print_r($css_files); } }

Output

The output of the above code is −

Array(
   [0] => Symfony\Component\Finder\SplFileInfo Object (
      [relativePath:Symfony\Component\Finder\SplFileInfo:private] =>
      [relativePathname:Symfony\Component\Finder\SplFileInfo:private] => test.css
      [pathName:SplFileInfo:private] => C:\xampp\htdocs\laraveltest\public\css\test.css
      [fileName:SplFileInfo:private] => test.css
   )
)

Example 4

To get all files from public folder using scandir() method −

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller{ public function index() { $path = public_path(); $files = scandir($path); print_r($files); } }

Output

The output of the above code is −

Array(
   [0] => .
   [1] => ..
   [2] => .htaccess
   [3] => css
   [4] => favicon.ico
   [5] => images
   [6] => index.php
   [7] => robots.txt
)

Updated on: 30-Aug-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements