Page 1 of 1
how do i read content of a folder?
Posted: Sun Jun 02, 2002 11:43 am
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
thanks guys, if someone can help i would be really greateful.

Posted: Sun Jun 02, 2002 12:26 pm
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.
Posted: Sun Jun 02, 2002 1:54 pm
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
Posted: Mon Jun 03, 2002 10:36 am
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

Posted: Mon Jun 03, 2002 11:26 am
by jason
Actually, all these things can be used for what you want. They just do it in a loop.
Posted: Mon Jun 03, 2002 5:46 pm
by qads
if you can tell me how then that would be great cos i tried,i can't do it
thanks
Posted: Mon Jun 03, 2002 6:03 pm
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)) {
while($file = readdir($handle)) {
if ($file != "." && $file != ".." && $file != "index.php") {
$file2 = substr("$file", 0, -4);
echo "<tr><td><a href="$file">$file2</a></td></tr>";
}
}
closedir($handle);
}
?>
</table>
try editing the root to your liking..
Posted: Mon Jun 03, 2002 8:08 pm
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))
{
if ($file != "." && $file != "..")
{
print('<tr><td><a href="'.$_SERVERї'PHP_SELF'].'?file='.urlencode($file).'">');
if ( $pos = strrpos($file, '.') )
$file = substr($file, 0, $pos);
print("$file</a></td></tr>");
}
}
closedir($handle);
?></table></body></html>
btw. no php tag anymore?

Posted: Mon Jun 03, 2002 8:48 pm
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")
Posted: Mon Jun 03, 2002 9:33 pm
by volka
I have nothing to hide

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?

Posted: Mon Jun 03, 2002 10:11 pm
by hob_goblin
it looks nicer

Posted: Tue Jun 04, 2002 4:33 am
by qads
Posted: Tue Jun 04, 2002 4:59 am
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.

Posted: Tue Jun 04, 2002 5:12 am
by volka
jo, and there's no need for the index.php to reside in the same directory as well

Posted: Thu Jun 20, 2002 5:45 pm
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?