How to access the public directory in Laravel?

In Laravel, you can access files in the public directory using the File facade or PHP's builtin functions. The public directory is where Laravel stores assets like images, CSS, and JavaScript files that need to be directly accessible via web URLs.

Make sure to import the File facade in your controller: use Illuminate\Support\Facades\File;

Laravel Public Directory Structure public/ images/ css/ js/ flower.jpg app.css app.js Use File::allFiles() or scandir() to access directory contents

Using File::allFiles() Method

The File::allFiles() method returns detailed file information as SplFileInfo objects ?

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class UserController extends Controller {
   public function index() {
      $images = File::allFiles(public_path('images'));
      print_r($images);
   }
}
?>
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
   )
)

Accessing Specific File Types

You can target specific directories by passing the folder name to public_path() ?

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class UserController extends Controller {
   public function index() {
      $css_files = File::allFiles(public_path('css'));
      print_r($css_files);
   }
}
?>
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
   )
)

Getting All Public Files

To retrieve all files from the entire public directory ?

<?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);
   }
}
?>
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(
      [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(
      [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
   )
)

Using scandir() Alternative

For simpler file listing without detailed metadata, use PHP's scandir() function ?

<?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);
   }
}
?>
Array
(
   [0] => .
   [1] => ..
   [2] => flower.jpg
)

Conclusion

Use File::allFiles() when you need detailed file information and scandir() for simple file name listings. The public_path() helper ensures correct path resolution across different environments.

Updated on: 2026-03-15T10:06:32+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements