Awar Pulldozer
Awar Pulldozer

Reputation: 1101

Show multiple files inside storage folder with unknown names Laravel

I have a laravel project and I have some folders inside storage folders like this

storage\app\public\archives\user_id\file1    
storage\app\public\archives\user_id\file2    
storage\app\public\archives\user_id\file3

now, I don't know the name of the files and I don't know the extensions of them also how can I run foreach based on user_id to show all files inside user_id folder like this example.

$files  = Storage::get(); // get files inside user_id folder only
foreach($files as $file)
{
    // fun my code here 
}

Thanks.

Upvotes: 0

Views: 335

Answers (1)

Mozammil
Mozammil

Reputation: 8750

I would use the File facade to achieve this.

For example:

$files = File::files(storage_path("app/public/archives/{$user->id}"));

Replace $user->id with your actual user id.

This will return an array of SplFileInfo objects, which means you can now do this:

foreach($files as $file) {
    echo $file->getBasename(); 
}

Check the documentation on SplFileInfo to have an idea of what's possible.

Upvotes: 1

Related Questions