Jump to content
xisto Community
sxyloverboy

How To List Files In A Directory + Subdirectory And Then Use Them.

Recommended Posts

So lets say i have folders called friends and work in a folder called pics.

how would i make a function that lists the files in those folderscalled images

kinda like this:

$directory = "./"function listfiles($directory) {//here should go the script to list the files in those directories. so that i can continue to work with them. like for example if there were images it would list all  the images and i could write a script to make a thumb of them and then save it into a thumb folder(not asking for all of that). but how would i make it list it. // basicly what i need is a loop for the files and a echo that lists them. }

the key point in my question is that it repects subdirectories. which is important forthe script i wanna make.

any ideas?

Share this post


Link to post
Share on other sites

Do you want to "hard-code" the sub-directory names? or have the sub-drectories found by the script in that directory? Makes a difference to the scripting involved.

Share this post


Link to post
Share on other sites

Is it acceptable that you have to tell the script which sub-diectories to list the contents of?

Share this post


Link to post
Share on other sites

hm i suppose i could do that myself with an array or? but what i want it to do automaticly. and thats what i cant do. i want it go through each of the folders where the script is run. and list the files. then look through the subfolders to see if theres any subfolders there ect. i hope this is not to hard to code. if it only works with one level of subfolders i suppose that would be ok too.

Share this post


Link to post
Share on other sites

There is a way to do that, and I thought I had a script for it, but I can't seem to find it. I'll keep looking for it. It is a recursive loop that does precisely what you want.

Share this post


Link to post
Share on other sites

hi, sxyloverboy, I have found this in http://php.net/. It should be your need.

Credit : msh at onliners dot dk

I would like to present these two simple functions for generating a complete directory listing - as I feel the other examples are to restrictive in terms of usage.function dirTree($dir) {   $d = dir($dir);   while (false !== ($entry = $d->read())) {	   if($entry != '.' && $entry != '..' && is_dir($dir.$entry))		   $arDir[$entry] = dirTree($dir.$entry.'/');   }   $d->close();   return $arDir;}function printTree($array, $level=0) {   foreach($array as $key => $value) {	   echo "<div class='dir' style='width: ".($level*20)."px;'> </div>".$key."<br/>\n";	   if(is_array($value))		   printTree($value, $level+1);   }}Usage is as simple as this:$dir = "<any directory you like>";$arDirTree = dirTree($dir);printTree($arDirTree);It is easy to add files to the tree also - so enjoy.

Share this post


Link to post
Share on other sites

hm that one dosent really work. it just shows a line called array. and yes i made the directory name right i tried a number of possiblities/pics/, /pics, ./pics, ./, /, pics/, picsdont know what else i could try but ive come to the conclusion that this script is faulty :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.