0
votes

I wanna display all files of a directory in the laravel application here is the error occurs about Finder can anyone help me with this.

Class 'Illuminate\Support\Facades\Finder' not found

Code

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Resource;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Finder;

public function index()

    {   
        $dir=base_path('resources/views/t/');
        return view('/admin/fileshandel/index')->with('allfiles' , 
        iterator_to_array(Finder::create()->files()->in($dir), false));
    }
2
You can check all the available classes here: laravel.com/api/5.8/Illuminate/Support/Facades.html There isn't a Finder class. (Note: Check 5.9 and dev versions also)Tim Lewis
thanks for your comment problem solved. I want one more thing this code now shows me filename with the whole URL but I wanna display only file name?? do you know how can I do this.Bilawal Awan
Nope, no idea. If you have a new question, ask a new question. This one has an answer, so accept that one (when you can) to close this. Best to keep questions focused.Tim Lewis
ok also done only using {{basename($name);}}Bilawal Awan

2 Answers

1
votes

Finder is under the Symfony namespace.

use Symfony\Component\Finder\Finder; would be correct.

1
votes

There is no Finder facade in laravel from version 4.0 till 5.8, if you need Symfony\Component\Finder\Finder is used by the File facade in this functions:

/**
 * Get an array of all files in a directory.
 *
 * @param  string  $directory
 * @param  bool  $hidden
 * @return \Symfony\Component\Finder\SplFileInfo[]
 */
public function files($directory, $hidden = false){}

/**
 * Get all of the files from the given directory (recursive).
 *
 * @param  string  $directory
 * @param  bool  $hidden
 * @return \Symfony\Component\Finder\SplFileInfo[]
 */
public function allFiles($directory, $hidden = false){}

/**
 * Get all of the directories within a given directory.
 *
 * @param  string  $directory
 * @return array
 */
public function directories($directory){}

So you can use these functions:

File::files($directory, $hidden = false);
File::allFiles($directory, $hidden = false);
File::directories($directory);

AFAIR even Illuminate\Support\Facades\Resource does not exist in laravel.

To get all files and directories in a directory you can try this:

$files = File::files(base_path('resources/views/t/'));
$directories = File::directories(base_path('resources/views/t/'));