Forum Moderators: coopster

Message Too Old, No Replies

Getting subdirectory contents with PHP4

how to go about returning the contents of a given directory.

         

WibbleWobble

10:29 am on May 19, 2003 (gmt 0)

10+ Year Member



Uh; I've been tasked with the monumental task of making a website more dynamic and useful; one of the things I'm required to do is create a php script that will read the contents of a subdirectory, so we can simply plonk reports and papers and whatnot in the subdir and have them viewable.
Currently, I've only got as far as listing the current directory - the one the script resides in.

//code begins
$getdir = dir(".");
print("<UL style='text-align: left;'>\n");
while($entry = $getdir->read())
{
if ($entry!= "." && $entry!= ".." && $entry!= "index.php" && $entry!= "default.php")
{ print("<LI><a href=$entry>$entry \n</a>"); }
}

this is the code I'm using, it was shamelessly tea-leafed from some php site, though I can't find it again. Anyway, the PHP manual seems to imply that I can set dir("/etc"); and have it list the contents of that tree, but doing so brings up:
Fatal error: Call to a member function on a non-object in path to htdocs/user on line 25.
[25 in the fully formatted script is the while statement]

Can anyone begin to shed some light on it? (should I be using readdir? etc)

Cheers.

WibbleWobble

2:37 pm on May 19, 2003 (gmt 0)

10+ Year Member



Figured it out. It was simple, really. I was trying to list /folder when I should've been trying to list the absolute one that the error reports, which is something like users/www/random-number/string/htdocs.

One problem down, millions more to go.

WibbleWobble

10:43 am on May 20, 2003 (gmt 0)

10+ Year Member



Alrighty, related issue. I need to filter out certain filetypes, or filter in only certain filetypes, it amounts to the same. Only I don't know where to begin. I'd like only .html/htm files to be displayed within the above script. I've looked at the if $entry!=Ideally, I'd like to be able to print the filesizes too, but thats less of an issue. I'm off to read more of the PHP manual :\

BCMG_Scott

3:05 pm on May 20, 2003 (gmt 0)

10+ Year Member



you could use either the strstr or stristr functions (in the strings function category) or use the *reg functions in the Regexp function list (see php.net documentation). I would favor using the regular expression functions, but that's because I started in Perl and use regexp often. If you are a beginner it may be easier to use the strstr and stristr functions.

Scott Geiger

jatar_k

3:07 pm on May 20, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what about using an array and then count .htm occurences. It should be only one really but will also trap .html.

$checkarr = array(".","..","index.php","default.php");
if (!in_array($entry,$checkarr) && substr_count ($entry,".htm") >= 1)
print("<LI><a href=$entry>$entry \n</a>");