how do i read content of a folder?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

how do i read content of a folder?

Post by qads »

hey guys,
i would like to know how i can read a folder, i have folder called "scripts" which has .zip files in it, i would like to read the file names in "scripts" folder and then show them on a page.

by the way, can i remove ".zip" bit and just show the filename?

e.g. filename = password.php
Show filename = Password

i hope i am makeing sense here :roll:
thanks guys, if someone can help i would be really greateful. :D
User avatar
fatal
Forum Contributor
Posts: 118
Joined: Sat Apr 20, 2002 10:47 am
Location: East Coast

Post by fatal »

You can read directorys by these functions:

http://www.php.net/manual/en/function.opendir.php
http://www.php.net/manual/en/function.readdir.php
http://www.php.net/manual/en/function.closedir.php

And to get it to just display the file name i would recomend puting the contents through some regular expression functions.
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post by sam »

Here is a snip of my random image code (modded for you):

Code: Select all

$i = 0;
$dir = opendir("icons");
while(($im = readdir($dir))) {
    if($im != ".." && $im != "."){
       $imageї$i] = substr($im,0,strlen($im)-4);
       $i++;
    }
}  
closedir($dir);
It just loads the names of the file into the $image array without the last 4 char (".ext").

Cheers Moe
Last edited by sam on Mon Jun 03, 2002 8:34 pm, edited 1 time in total.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

hmm, sorry i forgot to tell you that i need to read 1 file at a time so i can display it as a link, these codes show all the files in the folder so i can't use them as links.

but thanks for these :)
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Actually, all these things can be used for what you want. They just do it in a loop.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

if you can tell me how then that would be great cos i tried,i can't do it :cry:
thanks
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

<table>
<? 


$root = "/usr/local/psa/home/vhosts/ice-on-fire.net/web_users/team/tuts";
 if ($handle = opendir($root)) &#123;
   while($file = readdir($handle)) &#123;
     if ($file != "." && $file != ".." && $file != "index.php") &#123;
$file2 = substr("$file", 0, -4);
      echo "<tr><td><a href="$file">$file2</a></td></tr>";
     &#125;
   &#125;
   closedir($handle);
 &#125; 
?>
</table>
try editing the root to your liking..
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

just two little improvments.
  • not all extension are 3/4 characters long i.e. .js for Javascript
  • to be an the safe side use urlencode for the link

Code: Select all

<html><body><table><?php
$root = "./"; 
$handle = opendir($root) or die('cannot open directory');
while($file = readdir($handle))
&#123; 
	if ($file != "." && $file != "..")
	&#123; 
		print('<tr><td><a href="'.$_SERVER&#1111;'PHP_SELF'].'?file='.urlencode($file).'">');
		if ( $pos = strrpos($file, '.') )
			$file = substr($file, 0, $pos);
		print("$file</a></td></tr>");
	&#125; 
&#125; 
closedir($handle); 
?></table></body></html>
btw. no php tag anymore? :cry:
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

in your code, i would also do this...assuming the name of the file is index.php..

you would find this line and edit it:

if ($file != "." && $file != ".." && $file != "index.php")
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I have nothing to hide :D
hiding the script file's name is useless if you let the script display the contents of it's own directory since any user can see it anyway - it's in the link, isn't it? ;)
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

it looks nicer :-P
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

:D :D :D :D :D :D :D :D :D :D :D :D
thanks alot guys, that helped me alot.
:P :P :P :P :P :P :P :P
Tiimmy
Forum Commoner
Posts: 38
Joined: Sat Apr 27, 2002 1:56 am
Location: Australia
Contact:

Post by Tiimmy »

Well seeing as he's displaying the contents of a directory for scripts that he wants people to be able to download (I'm assuming), then there's no point in displaying index.php; to the user, it's just something useless that doesn't belong there. :lol:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

jo, and there's no need for the index.php to reside in the same directory as well ;)
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

but how would you make it list just certin file types? with that script, you have to desiginate specific files to exclude from being listed. I need a script that shows only .inc files, so what would I add to that script?
Post Reply