In Laravel 5, how can I get a list of all the files in a shared folder?

I would like to automatically generate a list of all the images in my shared folder, but I cannot find any object that could help me.

The Storage class seems like a good candidate for the job, but it allows me to search for files in a storage folder that is outside the shared folder.

+21
php laravel laravel-5 storage
source share
6 answers

You can create another drive for the Storage class. That would be the best solution for you, in my opinion.

In config / filesystems.php in the disk array add the desired folder. public folder in this case.

  'disks' => [ 'local' => [ 'driver' => 'local', 'root' => storage_path().'/app', ], 'public' => [ 'driver' => 'local', 'root' => public_path(), ], 's3' => '....' 

Then you can use the Storage class to work in your shared folder as follows:

 $exists = Storage::disk('public')->exists('file.jpg'); 

The $ exists variable will indicate if file.jpg exists inside the public folder, because Disk Storage points to the project’s public folder.

You can use all methods of the Session from the documentation using your custom disk. Just add part of the disk ('public').

  Storage::disk('public')-> // any method you want from 

http://laravel.com/docs/5.0/filesystem#basic-usage

+29
source share

Storage::disk('local')->files('optional_dir_name');

or

array_filter(Storage::disk('local')->files(), function ($item) {return strpos($item, 'png');});

Note that laravel disk has files() and allfiles() . allfiles is recursive.

+20
source share

Consider using glob. There is no need to reassign PHP barebones with helper classes / methods in Laravel 5.

 <?php foreach (glob("/location/for/public/images/*.png") as $filename) { echo "$filename size " . filesize($filename) . "\n"; } ?> 
+12
source share

To view all files in a directory use this

  $dir_path = public_path() . '/dirname'; $dir = new DirectoryIterator($dir_path); foreach ($dir as $fileinfo) { if (!$fileinfo->isDot()) { } else { } } 
+1
source share

To see all the images in your public directory, try this: see here http://php.net/manual/en/class.splfileinfo.php

  function getImageRelativePathsWfilenames(){ $result = []; $dirs = File::directories(public_path()); foreach($dirs as $dir){ var_dump($dir); //actually string: /home/mylinuxiser/myproject/public" $files = File::files($dir); foreach($files as $f){ var_dump($f); //actually object SplFileInfo //object(Symfony\Component\Finder\SplFileInfo)#628 (4) { //["relativePath":"Symfony\Component\Finder\SplFileInfo":private]=> //string(0) "" //["relativePathname":"Symfony\Component\Finder\SplFileInfo":private]=> //string(14) "text1_logo.png" //["pathName":"SplFileInfo":private]=> //string(82) "/home/mylinuxiser/myproject/public/img/text1_logo.png" //["fileName":"SplFileInfo":private]=> //string(14) "text1_logo.png" //} if(ends_with($f, ['.png', '.jpg', '.jpeg', '.gif'])){ $result[] = $f->getRelativePathname(); //prefix your public folder here if you want } } } return $result; //will be in this case ['img/text1_logo.png'] } 
0
source share

Please use the following code and get all the subdirectories of a specific folder in the shared folder. When someone clicks on a folder, the files inside each folder are displayed in it.

Controller file

  public function index() { try { $dirNames = array(); $this->folderPath = 'export'.DS.str_replace( '.', '_', $this->getCurrentShop->getCurrentShop()->shopify_domain ).DS.'exported_files'; $getAllDirs = File::directories( public_path( $this->folderPath ) ); foreach( $getAllDirs as $dir ) { $dirNames[] = basename($dir); } return view('backups/listfolders', compact('dirNames')); } catch ( Exception $ex ) { Log::error( $ex->getMessage() ); } } public function getFiles( $directoryName ) { try { $filesArr = array(); $this->folderPath = 'export'.DS.str_replace( '.', '_', $this->getCurrentShop->getCurrentShop()->shopify_domain ).DS.'exported_files'. DS . $directoryName; $folderPth = public_path( $this->folderPath ); $files = File::allFiles( $folderPth ); $replaceDocPath = str_replace( public_path(),'',$this->folderPath ); foreach( $files as $file ) { $filesArr[] = array( 'fileName' => $file->getRelativePathname(), 'fileUrl' => url($replaceDocPath.DS.$file->getRelativePathname()) ); } return view('backups/listfiles', compact('filesArr')); } catch (Exception $ex) { Log::error( $ex->getMessage() ); } } 

Route (Web.php)

 Route::resource('displaybackups', 'Displaybackups\BackupController')->only([ 'index', 'show']); 

Route :: get ('get-files / {directoryName}', 'Displaybackups \ BackupController @getFiles');

Browse Files - List Folders

 @foreach( $dirNames as $dirName) <div class="col-lg-3 col-md-3 col-sm-4 align-center"> <a href="get-files/{{$dirName}}" class="btn btn-light folder-wrap" role="button"> <span class="glyphicon glyphicon-folder-open folderIcons"></span> {{ $dirName }} </a> </div> @endforeach 

View - file list

 @foreach( $filesArr as $fileArr) <div class="col-lg-2 col-md-3 col-sm-4"> <a href="{{ $fileArr['fileUrl'] }}" class="waves-effect waves-light btn green folder-wrap"> <span class="glyphicon glyphicon-file folderIcons"></span> <span class="file-name">{{ $fileArr['fileName'] }}</span> </a> </div> @endforeach 
0
source share

All Articles