Jump to content

Array of filenames from directory?


newbtophp

Recommended Posts

Hi,

 

I've tried various methods (ie. glob etc.) but haven't had any success, the purpose is to scan through all directories for php files, then place those php files along with their path ($dir/$filename) into an array (to use elsewhere)

 

So if I have the following file/directory structure (example):

 

--forums:
---topic.php
---forum.php

--func:
---functions.php

 

Would produce the following array:

 

$files = array('topic.php' => 'forums/topic.php',

              'forum.php' => 'forums/forum.php'

              'functions.php' => 'func/functions.php');

Link to comment
Share on other sites

Assuming no files are named the same.

 

Try something like this:

function phpFiles($baseDir,&$files){

$handle = opendir($baseDir);
while (false !== ($file = readdir($handle))){ 

	$file_path = $baseDir . "/$file";

	//check for directory
	if(is_dir($file_path)){

		phpFiles($file_path, $files);

	} else {
		//check for php file
		$extension = strtolower(substr(strrchr($filename, '.'), 1)); 
  			if($extension == 'php'){ 
   				 // add to array
   				 $files[$file] = $file_path;
  			} 
  		}
} 


closedir($handle);

}

$file_array = array();

phpFiles('baseDir',$file_array);

 

I haven't tested it but it should be a good start.

 

Link to comment
Share on other sites

You can't use that array structure. It's possible that two files have the same name but different paths, and you can't have two elements with the same index (obviously).

 

Their won't be duplicates/same names, as I will be creating the file/filenames to ensure their aint.

Link to comment
Share on other sites

Why not just store the entire path and not the filename? You can easily get the filename later using pathinfo. Here's a more simplistic approach using the RecursiveDirectoryIterator:

 

$files = array();
$it = new RecursiveDirectoryIterator('path/to/dir');
foreach(new RecursiveIteratorIterator($it) as $path) {
if(pathinfo($path, PATHINFO_EXTENSION) == 'php') {
	$files[] = $path;
}
}

 

If you're absolutely sure that their won't be duplicates you can change this:

 

$files[] = $path;

to:

$files[pathinfo($path, PATHINFO_BASENAME)] = $path;

 

But I would not recommend it. If you come back to this code 6 months later chances are you aren't going to remember that you cannot duplicate files, and when you do put in a duplicate file you'll be beating yourself up when you can't find the problem. It's always better to code safe and leave less potential for problems later.

 

If anything it would be a better idea to have the index as the path and the filename as the value because you know that filenames can not be duplicated. That makes much more sense logically as well.

Link to comment
Share on other sites

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.