View Full Version : PHP Directory Spider

Sep 13, 2006, 12:04 PM

I'm making a website for my band and I'm trying to make it so that my media page can list all of the files in a certain directory that have a certain extension, and link to them. Is there any way of doing that in PHP?



EDIT: The website in question is http://www.theautonomy.co.uk/

Sep 13, 2006, 02:15 PM
Heres some slightly adapted code I was using for basically the same purpose.

function get_files($directory, $file_type) {

$files = array();
$hidden = array();

if (is_dir($directory)) {
if ($directory_handle = opendir($directory)) {
while (($file = readdir($directory_handle)) !== false) {
if (substr($file, 0, 1) == '.') {
$hidden[] = $file;
else {
if (strstr($file, $file_type)) {
$files[] = $file;
else {
echo "Specified directory doesn't exist.";

return $files;

It's not exactly perfect and I'm sure it can be improved in many ways but it'll get you started on what you want to achieve.

Something to take note off, is that the function determines the file type by searching the filename using the strstr() function which is case sensitive, so take care when your specifying the file type. Because its searching the filename for the file type you should probably always include the period when specifying the file type. For example you have a file named mympg.php and you said to look for .mpg files, the function won't return that file as being of the specified type.

Also it filters out hidden files and directories by looking for filenames that begin with a period. It's pretty basic and its only been tested on a server running Linux and I don't really know much about hidden files under different operating systems so use at your own risk.

Hope this helps.


Sep 13, 2006, 02:54 PM
Thanks - I'm not an experienced user of PHP, so would you be able to elaborate on how I could use that? Right now I've got a 'controller' file containing that function, and an instance of that function containing the variables. I then include() that file on the page, but for output I just get the entire contents of the controller file. I've never used functions before in PHP - any chance you could help?

Thanks again for the help

EDIT: Okay, I think I've got it, just one thing - how do I define the 'file type' variable? When I use ".mp3" it doesn't show any results

Sep 13, 2006, 03:12 PM
Ok heres an example on how you would use the function.

$files = get_files('directory/path/', '.mpg');

foreach ($files as $file) {
echo "<a href=\"directory/path/$file\">$file</a>";
echo '<br />';

Which would produce something like this:

<a href="example/directory/file.mpg">file.mpg</a>
<a href="example/directory/file2.mpg">file2.mpg</a>

and so on depending on how many files there were.

You've got the controller part right but it would be easier just to call the function against a variable in the main file and include the file containing the code as you said but it doesn't really matter. If you keep it as you have it the now then all you have to do is get rid of the first line in the above code and put it in the controller file.

Hope this helps you further.