Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.