majik92 Posted March 17, 2009 Share Posted March 17, 2009 Hello I can get all the files from a folder into an array, but how do get the files from subfolders within that folder into the array too. For example there are these 3 files: original/file1.rar original/file2.rar original/bob/file3.rar I am trying to get the above in to one array so that: $array[0] is "original/file1.rar" $array[1] is "original/file2.rar" and $array[2] is "original/bob/file3.rar" Thanks Quote Link to comment https://forums.phpfreaks.com/topic/149886-all-files-from-a-folder-and-subfolders-into-an-array/ Share on other sites More sharing options...
majik92 Posted March 17, 2009 Author Share Posted March 17, 2009 can a mod delete this thread. firefox lagged and i accidently posted twice... sorry Quote Link to comment https://forums.phpfreaks.com/topic/149886-all-files-from-a-folder-and-subfolders-into-an-array/#findComment-787122 Share on other sites More sharing options...
Renlok Posted March 17, 2009 Share Posted March 17, 2009 http://www.phpfreaks.com/forums/index.php/topic,243563.msg1137390.html#msg1137390 Post Deleted, double post $array = array(); if (is_dir('original')) { if ($dir = opendir('original')) { while (($file = readdir($dir)) !== false) { if (is_file('original/' . $file)) $array[] = 'original/' . $file; elseif(is_dir('original/' . $file)) { if ($dir = opendir('original')) { while (($file2 = readdir($dir)) !== false) { if (is_file('original/' . $file . '/' . $file2)) $array[] = 'original/' . $file . '/' . $file2; } closedir($dir); } } } closedir($dir); } } Quote Link to comment https://forums.phpfreaks.com/topic/149886-all-files-from-a-folder-and-subfolders-into-an-array/#findComment-787125 Share on other sites More sharing options...
iarp Posted March 17, 2009 Share Posted March 17, 2009 On the same idea, what about if you didn't know how many sub directories there were? Anyway to just scan the dir and get everything? Quote Link to comment https://forums.phpfreaks.com/topic/149886-all-files-from-a-folder-and-subfolders-into-an-array/#findComment-787142 Share on other sites More sharing options...
laffin Posted March 17, 2009 Share Posted March 17, 2009 function recdir($base) { $files=array(); if(is_dir($base) &&$ $dh=opendir($base)) { while($file=readdir($dh)) { if($file=='.' || $file== '..') continue; if(is_dir($file)) { $subfiles=recdir($base.'/'.$file); $tfiles=array_merge($files,$subfiles); $files=$tfiles; } else { $files[]=$base.'/'.$file; } } } return $files; } I think that shud work May require tweaking, not tested Quote Link to comment https://forums.phpfreaks.com/topic/149886-all-files-from-a-folder-and-subfolders-into-an-array/#findComment-787147 Share on other sites More sharing options...
samshel Posted March 17, 2009 Share Posted March 17, 2009 laffin beats me in it almost same code...i just put more time to test it and it works This code works till 3 levels of sub dirs, pl feel free to modify it as per convinience. <?php $path = "/tmp"; function getFileList($path, $level=1) { $arrFiles = scandir($path); $arrFileList = array(); $intCount = count($arrFiles); for($i=0;$i<$intCount;$i++) { if($arrFiles[$i] != "." && $arrFiles[$i] != "..") { if(is_dir($path."/".$arrFiles[$i])) { if($level > 3) return $arrFileList; $arrFileList = array_merge($arrFileList, getFileList($path."/".$arrFiles[$i],$level+1)); } else { $arrFileList[] = $path."/".$arrFiles[$i]; } } } return($arrFileList); } var_dump(getFileList($path)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/149886-all-files-from-a-folder-and-subfolders-into-an-array/#findComment-787151 Share on other sites More sharing options...
majik92 Posted March 17, 2009 Author Share Posted March 17, 2009 Thanks samshel! It seems to work. I really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/149886-all-files-from-a-folder-and-subfolders-into-an-array/#findComment-787167 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.