dodgeitorelse3 Posted January 8, 2018 Share Posted January 8, 2018 I have this code which works fine for getting file names and sizes from directories found in a main directory. directory structure is: maps dir1 file1 file1 file3 dir2 file1 file1 file3 dir3 file1 file1 file3 sub-dir file1 file1 file3 The code I have is <?php //ini_set('max_execution_time', 0); function getFileListDir($resource_dir) { // array to hold return value $retvaldir = array(); // add trailing slash if missing if(substr($resource_dir, -1) != "/") $resource_dir .= "/"; // open pointer to directory and read list of files $d = @dir($resource_dir) or die("getFileListDir: Failed opening directory $resource_dir for reading"); while(false !== ($entrydir = $d->read())) { // skip hidden files if($entrydir[0] == ".") continue; if(is_dir("$resource_dir$entrydir")) { $retvaldir[] = array( "dir_name" => "$entrydir", "dir_size" => disk_total_space("$resource_dir$entrydir")); } elseif(is_readable("$resource_dir$entrydir")) { $retvaldir[] = array( "map_name" => "$entrydir", "map_size" => filesize("$resource_dir$entrydir")); } else { $retvaldir = "Empty Directory"; } } $d->close(); return $retvaldir; } ?> this works fine for everything except sub-dir, I can't seem to wrap my head around how to get file names and sizes from the sub-dir. I tried calling this function again at the point of sub-dir but it does nothing more than when I don't call it again. I know glob would be easier, however with all the code I currently have that works with the returned $rertvaldir, it would be a nightmare to change. Not asking for anyone to write the code for me, just a little nudge in right direction. Thank you. Quote Link to comment Share on other sites More sharing options...
dodgeitorelse3 Posted January 8, 2018 Author Share Posted January 8, 2018 sorry, my bad, I wasn't paying attention. There is a foreach statement for the directory structure I showed which then gets me an array from each directory using the function I showed. The function does its job for dir3. I am having trouble when I get to sub-dir as in this structure: dir3 file1 file1 file3 sub-dir file1 file1 file3 I couldn't edit previous post, my apologies. Quote Link to comment Share on other sites More sharing options...
requinix Posted January 8, 2018 Share Posted January 8, 2018 Here if(is_dir("$resource_dir$entrydir")) { $retvaldir[] = array( "dir_name" => "$entrydir", "dir_size" => disk_total_space("$resource_dir$entrydir")); } is where you handle subdirectories. I don't know what sort of array structure you want but somehow it will involve calling the function recursively, like "dir_contains" => getFileListDir("$resource_dir$entrydir") Quote Link to comment Share on other sites More sharing options...
dodgeitorelse3 Posted January 8, 2018 Author Share Posted January 8, 2018 thank you requinix, I played around with your thoughts and am close to what I need, However, the closest I come is getting all files from sub_dir and only last file from dir3. if(is_dir("$resource_dir$entrydir")) { // dir totals, dir3 = 18 (17 files and 1 sub_dir), sub_dir = 442 files, so goal is to find 459 files and 1 sub_dir // this line by itself finds 18 which is total of dir3 files plus 1 sub_dir // $retvaldir[] = array("dir_directory" => getFileListDir("$resource_dir$entrydir")); // this line by itself finds 443 which is total of sub_dir files which is 442 plus 1 file from dir3 which is last file out of 18 //$retvaldir = getFileListDir("$resource_dir$entrydir"); // these 2 lines together find 444, 1 is the sub_dir, 442 are the files in sub_dir and 1 is last file out of 17 files from dir3 $retvaldir = getFileListDir("$resource_dir$entrydir"); $retvaldir[] = array($retvaldir, "map_name" => "$entrydir", "map_size" => filesize("$resource_dir$entrydir")); } I tried different ways and got different results each time. Is this a pointer issue? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 8, 2018 Share Posted January 8, 2018 Sounds like you are wiping out your previous results in some way. Recursion is tricky. One has to be sure to keep global vars and local vars distinct and to be sure to capture the results of each call to the working function. Keep at it! Quote Link to comment Share on other sites More sharing options...
Solution kicken Posted January 8, 2018 Solution Share Posted January 8, 2018 if(is_dir("$resource_dir$entrydir")) { $retvaldir[] = array( "dir_name" => "$entrydir", "dir_size" => disk_total_space("$resource_dir$entrydir")); $retvaldir = array_merge($retvaldir, getFileListDir("$resource_dir$entrydir")); }That adds your current directory entry to the list, then appends all the entries found by the recursive call to the list. Quote Link to comment Share on other sites More sharing options...
dodgeitorelse3 Posted January 8, 2018 Author Share Posted January 8, 2018 well, that took the work out of it for me, thank you. I was trying to do the array_merger in the is_readable part. Thank you requinix, ginerjm and kicken. Quote Link to comment 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.