Reputation: 21
actully i get the folder and files from perticular path but its doesnt returns the value what i want.
im expecting the return value like:-
[{"filename":"19_0_0_0_0_1.jpg","RFI":19},
{"filename":"19_0_0_0_0_2.jpg","RFI":19},
{"filename":"19_20005_1_0_0_1.jpg","RFI":19},
{"filename":"19_20005_1_0_0_2.jpg","RFI":19},
{"filename":"19_20005_1_429_0_1.jpg","RFI":19},
{"filename":"19_20005_1_429_0_2.jpg","RFI":19},
{"filename":"19_20005_1_429_1_1.jpg","RFI":19},
{"filename":"19_20005_1_429_1_2.jpg","RFI":19},
{"filename":"19_20027_1_0_0_1.jpg","RFI":19}]
and its give me like this output :
[{"filename":[{"filename":"19_0_0_0_0_1.jpg","RFI":19},
{"filename":"19_0_0_0_0_2.jpg","RFI":19},
{"filename":"19_20005_1_0_0_1.jpg","RFI":19},
{"filename":"19_20005_1_0_0_2.jpg","RFI":19},
{"filename":"19_20005_1_429_0_1.jpg","RFI":19},
{"filename":"19_20005_1_429_0_2.jpg","RFI":19},
{"filename":"19_20005_1_429_1_1.jpg","RFI":19},
{"filename":"19_20005_1_429_1_2.jpg","RFI":19},
{"filename":"19_20027_1_0_0_1.jpg","RFI":19}],"RFI":19}]
this is my code:
$ldir = "D:\php\EIL_App\RFIImages";
$data = listFolderFiles($ldir,19);
print json_encode($data);
function listFolderFiles($dir,$pRFI)
{
$result = array();
foreach (new DirectoryIterator($dir) as $fileInfo){
if (!$fileInfo->isDot()){
$dataimg = $fileInfo->getFilename();
if($fileInfo->getFilename() == $pRFI){
if ($fileInfo->isDir()){
$dataimg = listFolderFiles($fileInfo->getPathname(),$pRFI);
}
array_push($result,array('filename'=>$dataimg,'RFI'=>$pRFI));
}
}
}
return $result;
}
Please give the suggestion what can i do???
Thanks in advance.
Upvotes: 1
Views: 73
Reputation: 20286
There's no need to complicate things
$prefix = 19;
$dir = "D:\php\EIL_App\RFIImages";
$data = glob("$prefix*.*",19); // it will give you array with all files matching pattern
// then you can do a loop
$arr = [];
foreach ($data as $filename) {
$arr[] = ['filename' => $filename, 'prefix' => $prefix];
}
echo json_encode($arr);
http://php.net/manual/en/function.glob.php
If you want to use iterator then use glob http://php.net/manual/en/globiterator.construct.php
Regarding your code
if ($fileInfo->isDir())
this makes no sense because it will never go to this if. It's because before you check if (!$fileInfo->isDot()){
and dot means either .
or ..
which applies to directories. If you want to do it recursively then you can use
Edit:
I've noticed you do some recursion if so then probably it'd be better to use ResursiveIterator
function listFolderFiles($dir, $prefix) {
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
$files = new RegexIterator($rii, "^$prefix.*", RegexIterator::GET_MATCH); // note prefix cannot have characters that are special to regex or they should be escaped
$arr = array();
foreach($files as $file) {
$arr[] = $file;
}
return $arr;
}
http://php.net/manual/en/class.recursivedirectoryiterator.php http://php.net/manual/en/directoryiterator.isdot.php
Upvotes: 1