Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Laravel, how can I obtain a list of all files in a public folder?

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.

like image 663
kant312 Avatar asked Jul 09 '15 11:07

kant312


People also ask

How do I see all php files in a specific folder?

The scandir() function in PHP is an inbuilt function that is used to return an array of files and directories of the specified directory. The scandir() function lists the files and directories which are present inside a specified path.

How do I get a list of files?

Start -> Run -> Type in “cmd” This will open the command window. Next I will have to move into the correct directory. On my computer, the default directory is on the C: drive, but the folder I want to list the files for is on the D: drive, the first thing I will see is the prompt “C:\>”.

What is __ DIR __ In Laravel?

__DIR__ is the current directory you are in, if you wanted to go back one step, you could use dirname. This is used such as; dirname(__DIR__);

How do I get files in Laravel?

If you have file object from request then you can simply get by laravel function. $extension = $request->file->extension(); dd($extension); If you have file object from request then you can simply get by laravel function.


2 Answers

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.

like image 194
igs013 Avatar answered Oct 13 '22 01:10

igs013


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.

like image 32
Tarek Adam Avatar answered Oct 13 '22 01:10

Tarek Adam