Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to access the public directory in Laravel?
To get the file list from a public directory you can make use of the File facade class. To work with it you need to include the File class using the following statement -
use Illuminate\Support\Facades\File;
Assume we have the following files in the public folder ?

Example 1
Following Program tries to retrieve all the images from the 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 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 2
To get all CSS files from public_folder you just need to poass the string 'css' to the public_path() method.
<?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 3
To get all files from public folder the code is as follows ?
$path = public_path(); $files = File::allFiles($path);
The $files variable is an array with details of all the files present in the public/ folder.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; 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 4
You can make use of scandir() to get all the files in the public/images folder. It can be done as shown below ?
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class UserController extends Controller { public function index() { $path = public_path('images'); $files = scandir($path); print_r($files); } }
Output
The output of the above code is ?
Array ( [0] => . [1] => .. [2] => flower.jpg )