I have images in my public
folder (NOT STORAGE) :
And I want to get all these files in a list and for each one, I do something ... How can I do that?
I have images in my public
folder (NOT STORAGE) :
And I want to get all these files in a list and for each one, I do something ... How can I do that?
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());
For more info, check the documentation.
Edit: The documentation is confusing. It seems, you would need to use the File
Facade instead. I will investigate a bit more, but it seems to be working now.
Also, the result will be an array of SplFileInfo objects.
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);
}
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);
}
Thanks @MyLibary :)
If you mean the public folder that is not in the STORAGE folder, but, if you want to handle this with Laravel Storage (Laravel Filesystem), You can define this folder as a Disk for filesystem and use it as follow.
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');
More details about Laravel Storage (Laravel Filesystem) is here: https://laravel.com/docs/5.8/filesystem
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',
],
...
],