Jump to content

getting file names/sizes from directories/sub-directories


dodgeitorelse3

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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")
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites


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.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.