Jump to content

Recommended Posts

I have this script...

// get sub folders in directory
function dir_list($d){
       foreach(array_diff(scandir($d),array('.','..')) as $f)if(is_dir($d.'/'.$f))$l[]=$f;
       return $l;
} 

// get files from folders
function file_list($d,$x){
       foreach(array_diff(scandir($d),array('.','..')) as $f)if(is_file($d.'/'.$f)&&(($x)?ereg($x.'$',$f):1))$l[]=$f;
       return $l;
} 

$dir = dir_list("modules");
$file = file_list("modules",".php");

$files = array_merge($dir,$file);

$count = count($files);
for($i=0;$i<$count;$i++)
{
echo $files[$i]."<br />";
}

 

my file/folder structure is...

modules--

a.php

b.php

c.php

f.php

z.php

  helloworld--

  helloworld.php

  test--

  test.php

 

I'm trying to get my script to grab all the files and their paths, and match them up.

so my current output should be(not exact same order)...

a.php

b.php

c.php

f.php

z.php

helloworld/helloworld.php

test/test.php

 

but instead my current output is...

helloworld

test

a.php

b.php

c.php

f.php

z.php

 

so I want the file paths to each folder, whether it's in the root directory, or in sub folders.

 

any help is GREATLY appreciated  :)

 

Regards, ACE

Link to comment
https://forums.phpfreaks.com/topic/163880-solved-file-paths/
Share on other sites

/**
* @param string $path The directory to get the tree of
* @param string $directorySeparator Optional alternate directory separator than the OS' default
* @param array $extensionLimit Limit return to specific extensions
* @param string $startPath Start part to strip off (internal)
*/
function getDirectoryTree($path, $directorySeparator = DIRECTORY_SEPARATOR, array $extensionLimit = array(), $startPath = null)
{
if (!is_dir($path)) {
	throw new InvalidArgumentException('The given path is not a directory.');
}

if (null === $startPath) {
	$startPath = $path;
}

$startPathLength = strlen($startPath) + 1;

$items = array();
foreach (new DirectoryIterator($path) as $item) {
	if ($item->isDot()) continue;

	if ($item->isDir()) {
		$items = array_merge($items, getDirectoryTree($item->getPathname(), $directorySeparator, $extensionLimit, $startPath));
	}
	else {
		$name = substr($item->getPathName(), $startPathLength);

		if (count($extensionLimit) && !in_array(pathinfo($name, PATHINFO_EXTENSION), $extensionLimit)) {
			continue;
		}

		if ($directorySeparator !== DIRECTORY_SEPARATOR) {
			$name = str_replace(DIRECTORY_SEPARATOR, $directorySeparator, $name);
		}

		$items[] = $name;
	}
}

return $items;
}

 

Then

foreach (getDirectoryTree('C:/projects/phpfreaks/application', '/', array('php')) as $item) {
echo $item . PHP_EOL;
}

outputs (on my laptop):

Bootstrap.php
models/Comments.php
models/Content.php
models/Feeds.php
models/ForumUsers.php
models/Snippets.php
models/User.php
models/Users.php
models/Votes.php
modules/default/controllers/CommentController.php
modules/default/controllers/ContentController.php
modules/default/controllers/ErrorController.php
modules/default/controllers/FeedController.php
modules/default/controllers/IndexController.php
modules/default/controllers/SearchController.php
modules/default/controllers/SnippetsController.php
modules/default/controllers/UserController.php
modules/default/controllers/VoteController.php
modules/default/views/helpers/ContentBody.php
modules/default/views/helpers/FormMultiText.php
modules/default/views/helpers/Image.php
modules/default/views/helpers/ParseData.php
modules/default/views/helpers/Plural.php
modules/default/views/helpers/Truncate.php

 

If you don't actually care that it's the absolute path or don't care about the directory separators, you can remove that for extra performance.

Link to comment
https://forums.phpfreaks.com/topic/163880-solved-file-paths/#findComment-864660
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.