View Full Version : Cataloging contents of a directory with PHP?
matbury
03-12-2008, 12:10 PM
Hi,
Is it possible to search a directory on a server to list all the files present in that directory?
i.e. In 'mydirectory' there are the following files:
/mydirectory/video.fla
/mydirectory/player.swf
/mydirectory/info.xml
/mydirectory/image1.jpg
/mydirectory/image2.jpg
/mydirectory/image3.jpg
/mydirectory/image4.jpg
/mydirectory/image5.jpg
/mydirectory/logo.gif
Can I get PHP to look in 'mydirectory' and retrieve a list of URLs? If so how?
CyanBlue
03-12-2008, 01:56 PM
Did a quick search from the Google and this came up...
http://www.spoono.com/php/tutorials/tutorial.php?id=10
and here is an edited version... You should really think of the security stuff if you plan to use this sort of script though... For example, if you use this without properly enable/disable the directory browsing on the important direcoties, you are showing everything you have to the world, if you know what I mean... ;)
<?
$folder = $_GET['directory'];
//define the path as relative
$path = "./" . $folder;
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
echo "Directory Listing of $path<br/>";
//running the while loop
while ($file = readdir($dir_handle))
{
if ($file == "." || $file == "..")
{
echo "<a href='$file'>$file</a><br/>";
}
else
{
echo "<a href='$path/$file'>$path/$file</a><br/>";
}
}
//closing the directory
closedir($dir_handle);
?>
matbury
03-12-2008, 02:40 PM
Thanks a million Cyan! I'll try it out.
On the security note - this'll only be accessible from an administrator's point of view i.e. username and password protected.
On another note, if I try to implement this from within a dynamic PHP site and I want to have the files directory outside that site folder, can I use absolute paths or a path constant, like '$PATH->'?
I'll go and check out the tutorial now...
matbury
03-12-2008, 03:23 PM
Hi again Cyan,
I tried out the script you posted above. It works fine. I didn't think of doing a Google search... me being stoopid!
I also found this script (below) which does a little bit more.
Thanks again, Cyan!
<?php
// open this directory
$myDirectory = opendir(".");
// get each entry
while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}
// close directory
closedir($myDirectory);
// count elements in array
$indexCount = count($dirArray);
Print ("</head><body>$indexCount files<br>\n");
// sort 'em
sort($dirArray);
// print 'em
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n");
// loop through the array of files and print them all
for($index=0; $index < $indexCount; $index++) {
if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
print("<td>");
print(filetype($dirArray[$index]));
print("</td>");
print("<td>");
print(filesize($dirArray[$index]));
print("</td>");
print("</TR>\n");
}
}
print("</TABLE>\n");
?>
CyanBlue
03-12-2008, 04:19 PM
Looks good... That one actually look somewhat similar to what I used on other project...
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.