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.
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 Session 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
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.
Consider using glob. No need to overcomplicate barebones PHP with helper classes/methods in Laravel 5.
<?php
foreach (glob("/location/for/public/images/*.png") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
?>
To list all files in directory use this
$dir_path = public_path() . '/dirname';
$dir = new DirectoryIterator($dir_path);
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
}
else {
}
}
To list all images in your public dir try this:
see here btw 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']
}
Please use the following code and get all the subdirectories of a particular folder in the public folder. When some click the folder it lists the files inside each folder.
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');
View 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 - List Files
@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