List of folder and files


#1

Hi i'm troubling. 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.


#2

It seems to me that the issue is sometimes $dataimg will contain just the current filename, and sometimes it will contain an array of filenames from a subfolder, if you call listFolderFiles() recursively. This seems to be what you show in the sample output, the first array entry contains an array of entries. Perhaps the solution is to merge the arrays if you have performed a recursive call, and push if you have not. I'm not that experienced with array functions though, there may be a better way. Do you need to indicate whether the files are in a subfolder?


#3

Hi jyotsnatamboli welcome to the forum

You could use DirectoryIterator if you made a few changes to the code. However, since you need recursion, IMHO you may as well use RecursiveDirectoryIterator instead and keep it as simple as possible.

http://php.net/manual/en/class.recursivedirectoryiterator.php


#4

This is a great library for navigating file hierarchies. It provides a standard interface with adapters for everything from local file systems to cloud servers like an amazon cdn and ftp servers.

http://flysystem.thephpleague.com/


#5

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.