In Laravel, how can I obtain a list of all files in a public folder?
Solution 1
You could create another disk for Storage class. This would be the best solution for you in my opinion.
In config/filesystems.php in the disks array add your desired folder. The public folder in this case.
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path().'/app',
],
'public' => [
'driver' => 'local',
'root' => public_path(),
],
's3' => '....'
Then you can use Storage class to work within your public folder in the following way:
$exists = Storage::disk('public')->exists('file.jpg');
The $exists variable will tell you if file.jpg exists inside the public folder because the Storage disk 'public' points to public folder of project.
You can use all the Storage methods from documentation, with your custom disk. Just add the disk('public') part.
Storage::disk('public')-> // any method you want from
http://laravel.com/docs/5.0/filesystem#basic-usage
Later Edit:
People complained that my answer is not giving the exact method to list the files, but my intention was never to drop a line of code that the op copy / paste into his project. I wanted to "teach" him, if I can use that word, how to use the laravel Storage, rather then just pasting some code.
Anyway, the actual methods for listing the files are:
$files = Storage::disk('public')->files($directory);
// Recursive...
$files = Storage::disk('public')->allFiles($directory);
And the configuration part and background are above, in my original answer.
Solution 2
Storage::disk('local')->files('optional_dir_name');
or just a certain type of file
array_filter(Storage::disk('local')->files(), function ($item) {
//only png's
return strpos($item, '.png');
});
Note that laravel disk has files()
and allfiles()
. allfiles
is recursive.
Solution 3
Consider using glob. No need to overcomplicate barebones PHP with helper classes/methods in Laravel 5.
<?php
foreach (glob("/location/for/public/images/*.png") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
?>
Solution 4
To list all files in directory use this
$dir_path = public_path() . '/dirname';
$dir = new DirectoryIterator($dir_path);
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
}
else {
}
}
Solution 5
You can use FilesystemReader::listContents
Storage::disk('public')->listContents();
Sample response...
[
[
"type" => "file",
"path" => ".gitignore",
"timestamp" => 1600098847,
"size" => 27,
"dirname" => "",
"basename" => ".gitignore",
"extension" => "gitignore",
"filename" => "",
],
[
"type" => "dir",
"path" => "avatars",
"timestamp" => 1600187489,
"dirname" => "",
"basename" => "avatars",
"filename" => "avatars",
]
]
kant312
Web developer based in Brussels - PHP / Vue.js / Laravel / Symfony
Updated on July 09, 2022Comments
-
kant312 almost 2 years
I'd like to automatically generate a list of all images in my public folder, but I cannot seem to find any object that could help me do this.
The
Storage
class seems like a good candidate for the job, but it only allows me to search files within the storage folder, which is outside the public folder. -
kant312 almost 9 yearsBonus for a more "laravelish" solution
-
Toskan over 5 yearsthis is a lengthy answer with 20 upvotes. Yet you don't answer the question of "list of all images in my public folder" and the laravel documentation apge doesn't really explain what Storage::allFiles() returns. Yes, an array, but what is inside the array? how do I get the filename and path from that?
-
Sweet Chilly Philly over 5 yearsthank you! i created a symbolic link so I can access photos via asset('storage/imgagName.jpg'), then foreached over Storage::disk('local')->files() to display them in a table on my view.
-
Scotty over 5 yearsMy thoughts also. They didn't answer the question.
-
Tarek Adam over 5 yearsI don't get how the author of the question accepted this answer.
-
kant312 about 5 yearsI accepted the answer because my question was more about how to use
Storage
on a public folder than the specifics of listing the files. So it solved my particular problem, but maybe the question does not reflect that enough. -
Tarek Adam about 5 years
storage_path('app')
does the slashes for you -
sgotre about 5 yearsAnd seems like the most efficient for local file system
-
Alaksandar Jesus Gene over 3 yearsPerfect solution
-
saber tabatabaee yazdi over 3 yearshow to convert it to laravel Image Object
-
Oboroten over 3 yearsisFile could be useful here. It is more understandable for me
-
Peter about 3 years!warning: This is still puling every single files and iterating through each file
-
Tarek Adam almost 3 years@Peter ya... that's array_filter(...) What is your recommendation?
-
Fabian S. over 2 yearsPlease consider adding explanation on your code.
-
Sfili_81 over 2 yearsCode without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked. See How to Answer.
-
axlotl over 2 yearsWhat is wrong with copy and paste? It's using a simple tool to save work, and that is what computers are for.
-
axlotl over 2 yearsNote: you must be using the 3rd party package ("Flysystem") linked at the top of this answer.