IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet
 
Reply to this topicStart new topic

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


sxyloverboy
no avatar
Super Member
*********
Group: Members
Posts: 302
Joined: 17-June 05
From: Frankfurt, Germany
Member No.: 8,358



Post #1 post May 21 2006, 08:23 PM
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:

CODE

$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?
Go to the top of the page
+Quote Post
jlhaslip
no avatar
Insert Custom Title Here
*******************
Group: [MODERATOR]
Posts: 4,903
Joined: 24-July 05
From: Linux, DOS and Windows…the good, the bad and the ugly
Member No.: 9,787
Spam Patrol
myCENT:81.50



Post #2 post May 21 2006, 08:43 PM
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.
Go to the top of the page
+Quote Post
sxyloverboy
no avatar
Super Member
*********
Group: Members
Posts: 302
Joined: 17-June 05
From: Frankfurt, Germany
Member No.: 8,358



Post #3 post May 22 2006, 02:41 PM
hmm..dont understand your question. ^^;
Go to the top of the page
+Quote Post
jlhaslip
no avatar
Insert Custom Title Here
*******************
Group: [MODERATOR]
Posts: 4,903
Joined: 24-July 05
From: Linux, DOS and Windows…the good, the bad and the ugly
Member No.: 9,787
Spam Patrol
myCENT:81.50



Post #4 post May 22 2006, 02:51 PM
Is it acceptable that you have to tell the script which sub-diectories to list the contents of?
Go to the top of the page
+Quote Post
sxyloverboy
no avatar
Super Member
*********
Group: Members
Posts: 302
Joined: 17-June 05
From: Frankfurt, Germany
Member No.: 8,358



Post #5 post May 22 2006, 04:07 PM
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.
Go to the top of the page
+Quote Post
jlhaslip
no avatar
Insert Custom Title Here
*******************
Group: [MODERATOR]
Posts: 4,903
Joined: 24-July 05
From: Linux, DOS and Windows…the good, the bad and the ugly
Member No.: 9,787
Spam Patrol
myCENT:81.50



Post #6 post May 22 2006, 04:20 PM
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.
Go to the top of the page
+Quote Post
sxyloverboy
no avatar
Super Member
*********
Group: Members
Posts: 302
Joined: 17-June 05
From: Frankfurt, Germany
Member No.: 8,358



Post #7 post May 22 2006, 06:02 PM
hm well id really appreciate if you found it. smile.gif
Go to the top of the page
+Quote Post
magiccode9
no avatar
Premium Member
********
Group: Members
Posts: 188
Joined: 1-November 05
From: python inside
Member No.: 13,683
myCENT:75.26



Post #8 post May 23 2006, 04:59 AM
hi, sxyloverboy, I have found this in www.php.net. It should be your need.

Credit : msh at onliners dot dk

CODE
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.
Go to the top of the page
+Quote Post
sxyloverboy
no avatar
Super Member
*********
Group: Members
Posts: 302
Joined: 17-June 05
From: Frankfurt, Germany
Member No.: 8,358



Post #9 post May 24 2006, 04:15 PM
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/, pics

dont know what else i could try but ive come to the conclusion that this script is faulty tongue.gif
Go to the top of the page
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No new   19 KansukeKojima 3,332 14th June 2009 - 05:27 PM
Last post by: k_nitin_r
No New Posts 4 zip_mc 13,280 22nd September 2009 - 10:26 AM
Last post by: akira550
No New Posts   4 LuciferStar 7,604 19th August 2004 - 01:39 AM
Last post by: LuciferStar
No New Posts   2 threepach 2,092 19th February 2008 - 08:30 PM
Last post by: threepach
No new   27 OpaQue 25,238 18th October 2007 - 05:33 AM
Last post by: rvalkass
No New Posts   2 reconraiders 1,810 1st October 2007 - 03:31 PM
Last post by: ivenms
No New Posts   3 Zenchi 4,856 13th October 2004 - 03:36 PM
Last post by: ElBandoneonWEbsite
No New Posts   3 X3r0X 4,652 1st October 2004 - 08:16 AM
Last post by: X3r0X
No New Posts   1 hansley 7,676 11th October 2004 - 03:54 AM
Last post by: Trystim
No New Posts   13 Lyon 5,376 8th April 2009 - 07:21 PM
Last post by: Lyon2
No New Posts   10 odomike 7,434 10th February 2005 - 08:22 PM
Last post by: round
No new   20 sargilla 10,664 1st November 2009 - 11:36 PM
Last post by: Soviet Rathe
No New Posts   0 twoq 4,314 7th November 2004 - 06:19 AM
Last post by: twoq
No New Posts   11 Albus Dumbledore 3,883 28th April 2009 - 02:28 PM
Last post by: iG-Voffkin
No New Posts   13 Lyon 3,120 1st November 2009 - 07:34 PM
Last post by: Soviet Rathe


 



RSS Open Discussion Time is now: 6th November 2009 - 09:38 PM

Web Hosting Powered by ComputingHost.com.