How to get files list from public folder Laravel

phplaravelfilesfolderpubliclistdirectoryobtainexamplexfile6

Last Update : 2023-09-22 UTC 11:44:16 AM

Answers of > How to get files list from public folder Laravel

If you mean the public folder that is not in w3coded php x the STORAGE folder, but, if you want to handle w3coded php x this with Laravel Storage (Laravel Filesystem), w3coded php x You can define this folder as a Disk for w3coded php x filesystem and use it as follow.,Edit: The w3coded php x documentation is confusing. It seems, you would w3coded php x need to use the File Facade instead. I will w3coded php x investigate a bit more, but it seems to be w3coded php x working now. ,Note : If you changed Public w3coded php x folder you can use base_path() in the Disk w3coded php x definition with relative Path as follow w3coded php x (Otherwise don't use it):, w3coded php x By clicking “Accept all cookies”, you w3coded php x agree Stack Exchange can store cookies on your w3coded php x device and disclose information in accordance w3coded php x with our Cookie Policy.

You could do this in one line:

use File;

$files = File::files(public_path());

// If you would like to retrieve a list of 
// all files within a given directory including all sub-directories    
$files = File::allFiles(public_path()); 

Solved! I've used this function and it's work :

// GET PUBLIC FOLDER FILES (NAME)
if ($handle = opendir(public_path('img'))) {

    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            echo $entry."<br>"; // NAME OF THE FILE
        }
    }
    closedir($handle);
}

In the filesystem config config/filesystems.php, disks part add this code:

'disks' => [

        ...

        'public_site' => [
            'driver' => 'local',
            'root' => public_path(''),
            'visibility' => 'public',
        ],

        ...
    ],

And then you can use this commands in your app:

$contents = Storage::get('img/file.jpg');

or to get list of files:

$files = Storage::files('img');

$files = Storage::allFiles('img');

Note : If you changed Public folder you can use base_path() in the Disk definition with relative Path as follow (Otherwise don't use it):

'disks' => [

        ...

        'public_site' => [
            'driver' => 'local',
            'root' => base_path('../../public'),
            'visibility' => 'public',
        ],

        ...
    ],

Solution 1 for Laravel


public function index()
{
    $path = public_path('test');
    $files = File::allFiles($path);

    dd($files);
}

Solution 2 for Laravel


public function index()
{
    $path = public_path('test');
    $files = File::files($path);

    dd($files);
}

Solution for PHP


public function index()
{
    $path = public_path('demo');
    $files = scandir($path);
    dd($files);
}

Current topics : How to get files list from public folder Laravel

Newly Added Questions

Similar Questions

Questions :

How To Group Array Key Value

Last Update : 2023-09-22 UTC 13:37:16 PM

Questions :

PhpStorm Warning For React Attributes In Jsx File With SCSS File

Last Update : 2023-09-22 UTC 13:37:03 PM

Questions :

Why Is The File Not Showing Up In Request.files And In Request.forms Instead?

Last Update : 2023-09-22 UTC 13:36:50 PM

Questions :

Proxying Assets From React App Directory In Slim Framework?

Last Update : 2023-09-22 UTC 13:36:32 PM

Questions :

Laravel 5.4 Can't Run “php Artisan Preset React” Comand

Last Update : 2023-09-22 UTC 13:36:24 PM

Questions :

How To Update Session Values Without Signing Out?

Last Update : 2023-09-22 UTC 13:36:08 PM

Questions :

Array Is Not Visible

Last Update : 2023-09-22 UTC 13:35:54 PM

Questions :

React Routing For Login Using Symfony

Last Update : 2023-09-22 UTC 13:35:46 PM

Questions :

Sanctum With React SPA Returning 419 Page Expired

Last Update : 2023-09-22 UTC 13:35:37 PM

Questions :

How Do I Import An Input String Into Another Page

Last Update : 2023-09-22 UTC 13:35:19 PM

Top
© 2023 W3CODED - All Rights Reserved.