- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 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 )
- Related Articles
- How to upload files in Laravel directly into the public folder?
- How to access images inside a public folder in Laravel?
- How to delete a file from the public folder in Laravel?
- How to get list of all files/folders from a folder in Java?
- Python - How to Merge all excel files in a folder
- How to read all files in a folder to a single file using Java?
- Python - Read all CSV files in a folder in Pandas?
- How to access the public directory in Laravel?
- How to list all files in a directory using Java?
- How do I list all files of a directory in Python?
- How to read multiple text files from a folder in Python?(Tkinter)
- How to write files to asset folder in android?
- How to get a list of registered route paths in Laravel?
- Java program to List all files in a directory recursively
- How to list down all the files available in a directory using C#?

Advertisements