php - Recursively crawl files and directories and return a multidimensional array -



php - Recursively crawl files and directories and return a multidimensional array -

so i've been trying create recursive file directory tree listing function. have of except bugs. such duplicate directory names because of code not going deep plenty in tree.

function ftpfilelist($ftpconnection, $path="/") { static $allfiles = array(); $contents = ftp_nlist($ftpconnection, $path); foreach($contents $currentfile) { if($currentfile !== "." && $currentfile !== ".."){ if( strpos($currentfile,".") === false || strpos($currentfile,"." === 0) ) { if(!$allfiles[$path][$currentfile]){ $allfiles[$path][$currentfile] = array(); } ftpfilelist($ftpconnection,$currentfile); }else{ if($currentpath !== "." && $currentpath !== "..") $allfiles[$path][] = $currentfile; } } } homecoming $allfiles; }

the returned array looks similar this

array(3) { [""]=> array(4) { [0]=> string(9) ".ftpquota" [1]=> string(9) ".htaccess" ["kms"]=> array(0) { } ["public_html"]=> array(0) { } } ["kms"]=> array(6) { [0]=> string(16) "admin_config.php" [1]=> string(8) "css.json" [2]=> string(10) "pages.json" ["php_includes"]=> array(0) { } ["site"]=> array(0) { } ["templates"]=> array(0) { } } ["public_html"]=> array(20) { [0]=> string(9) ".htaccess" [1]=> string(7) "404.php" ... } }

basically want this

.htaccess .ftpquota -public_html -folder2 -folder3 file.ext file2.ext -kms -folder4 file3.ext -folder5 -file4.ext file5.ext

hopefully can understand asking, need see wrong here, , how right index place $currentfile in because of fact it's search $allfiles[$path][$currentfile] won't correct. anyways need recursive function list files in array, directories indexes.

i don't think there's reason "$allfiles" variable should static.

also, you're using $currentpath variable isn't defined anywhere. trying accomplish variable?

try code instead (it still isn't perfect, should give plenty hint on how create real recursion):

function ftpfilelist($ftpconnection, $path="/") { $files = array(); $contents = ftp_nlist($ftpconnection, $path); foreach($contents $currentfile) { if($currentfile !== "." && $currentfile !== ".."){ if( strpos($currentfile,".") === false || strpos($currentfile,"." === 0) ) { $files[$path][$currentfile] = ftpfilelist($ftpconnection, $path.$currentfile.'/'); }else{ if($currentpath !== "." && $currentpath !== "..") $files[$path][] = $currentfile; } } } homecoming $files; }

php arrays recursion multidimensional-array

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -