Jump to content

Recommended Posts

Hey

 

I have a directory /folder (which is actually /usr/home/web/[customer id]/folder when it comes to working with opendir()) and I would like to populate an array with name and location of all files found in the directory and it's subdirectories. Below are an example of a directory structure and the array I want to create from it:

 

Directory structure:

+/folder

|--file1.ext

|--file2.ext

|

|-+/subdir1

|  |--file1.ext

|  |--file2.ext

|

|--+/subdir2

    |--file3.ext

    |--file4.ext

    |-+/subsubdir1

    |  |--file3.ext

    |  |--file4.ext

 

Desired array:

Array
(
    [0] => Array
        (
            [file] => file1.ext
            [link] => /folder/file1.ext
        )

    [1] => Array
        (
            [file] => file2.ext
            [link] => /folder/file2.ext
        )

    [2] => Array
        (
            [file] => file1.ext
            [link] => /folder/subdir1/file1.ext
        )


    [3] => Array
        (
            [file] => file2.ext
            [link] => /folder/subdir1/file2.ext
        )

    [4] => Array
        (
            [file] => file3.ext
            [link] => /folder/subdir2/file3.ext
        )


    [5] => Array
        (
            [file] => file4.ext
            [link] => /folder/subdir2/file4.ext
        )


    [6] => Array
        (
            [file] => file3.ext
            [link] => /folder/subdir2/subsubdir1/file3.ext
        )


    [6] => Array
        (
            [file] => file4.ext
            [link] => /folder/subdir2/subsubdir1/file4.ext
        )

 

 

This is what I have come up with so far:

get_files_in_dir.php:

<?PHP

function get_files_in_dir($dir) {

//Check if opendir() can use $dir and open it
if($handle = opendir($dir)) {

	//Loop through $dir
	$i = 0;
	while (false !== ($file = readdir($handle))) {
		//Don't bother with the . and .. file/dir
  		if($file != "." && $file != "..") {
  		
  			//The full path to the file (or directory if $file is a dir)
  			$link = $dir . "/" . $file;
  		
  			//Check if $file is a dir
  			if(is_dir($link)){
  				
  				//The file was a dir so loop through that one too
  				get_files_in_dir($link);
  		}
  			else{ 
  				//The file was not a dir so it gets stored in the
  				//array $files[]. Both it's filename and path
  				$files[$i]["file"] = $file;
  				$files[$i]["link"] = $link;
  				$i++;
  			}
  		}
  	}
  
  //Close the dir
  closedir($handle);

//Print the array, just for the test phase
  print_r($files);
}
else {
return false;
}
}


//call the function:

$dir = "/usr/home/web/web115029/random/folder";
get_files_in_dir($dir);
?>

 

The function is "live" here and you can see its output - remember to view the source of the page for readable layout:

http://wuhtzu.dk/random/get_files_in_dir.php

 

As you can see my function creates a new array for each directory it finds because each time it finds a directory it calls it self again. Have you go any suggestions on how to proceed from here to achieve my goal?

 

Best regards

Wuhtzu

 

 

 

 

I got it right:

 

<?PHP

function get_files_in_dir($dir) {


// Verify that $dir is a directory
if(is_dir($dir)) {


// $dirs_to_scan[] contains all the direcotries to scan. It begins by 
// containing only $dir (as specified in the function call, but gets
// populated as more directories are found inside the orginal $dir
$dirs_to_scan[] = $dir;

// count directories
$i = 0;

// count files
$j = 0;

// loop through $dirs_to_scan. Runs as long as $dirs_to_scan holds more entries than
// have already been scanned and counted by $i
while($i < count($dirs_to_scan)) {

	//Create handle for the current dir to scan
	$handle = opendir($dirs_to_scan[$i]);

	//Loop through the current dir ($dirs_to_scan[$i]
	while (false !== ($file = readdir($handle))) {
		//Don't bother with the . and .. file/dir
		if($file != "." && $file != "..") {

			//The full path to the file (or directory if $file is a dir)
			$link = $dirs_to_scan[$i] . "/" . $file;

			//Check if $file is a dir
			if(is_dir($link)){

				//$file was a dir so sign it up for scan
				$dirs_to_scan[] = $link;
			}
			else{ 
				//The file was not a dir so it gets stored in the
				//array $files[]. Both it's filename and path
				$files[$j]["filename"] = $file;
				$files[$j]["file"] = $link;

				//Add to the file count
				$j++;
			}

		}
	}
	//Close the currently open dir
	closedir($handle);

	//Add to the directory count
	$i++;
}


// Print the $dirs_to_scan array - JUST FOR THE TEST PHASE
print_r($dirs_to_scan);

// New lines to improve readability - JUST FOR THE TEST PHASE
echo "\n\n\n";

// Print the $files array - JUST FOR THE TEST PHASE
print_r($files);

}
else {
return false;
}

}


//call the function:
$dir = "/usr/home/web/web115029/random";
get_files_in_dir($dir);
?>

 

The function is "live" here:

 

http://wuhtzu.dk/random/get_files_in_dir_working.php (remember to view page source to get it readable)

 

Thanks to those who checked by...

 

Wuhtzu

Do you mean echo for each cycle in the loop?

 

Well if I just wanted to see the output there wouldn't be any point in storing it in an array. It's for a file search function, so the print_r() is just for the testing the script and print_r() just looks horrible if you do not look at the source :)

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.