Jump to content

Listing all files/directories via a page


stickynote427

Recommended Posts

How can I automatically list every file or directory that exists in the same directory as a certain page?

 

For example, if I have this:

 

>index.php

>picture1.png

>/images

 

on index.php, I want to show the text "Here is everything in this directory: picture1.png /images" or something like that.

 

I tried the example here, but all it does is return "Array" if I use "/" for $directory, and returns errors when I use a URL from my site.

Link to comment
https://forums.phpfreaks.com/topic/155756-listing-all-filesdirectories-via-a-page/
Share on other sites

Sorry, it's been a while, but I think I did this:

<?php
function dirList ($directory) 
{

    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // keep going until all files in directory have been read
    while ($file = readdir($handler)) {

        // if $file isn't this directory or its parent, 
        // add it to the results array
        if ($file != '.' && $file != '..')
            $results[] = $file;
    }

    // tidy up: close the handler
    closedir($handler);

    // done!
    return $results;

}
echo dirList("/");
?>

EDIT:

For $directory, I just tried a server path, which was successful. Using print_r(), I could see all of the values of the array.

 

Now, how can I only add the files that are not hidden (.htaccess, etc. should not be listed) and are only of a certain file type?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.