stickynote427 Posted April 26, 2009 Share Posted April 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/155756-listing-all-filesdirectories-via-a-page/ Share on other sites More sharing options...
mikesta707 Posted April 26, 2009 Share Posted April 26, 2009 did you just copy and paste that code, or alter it some how? post your code, because the code from that function should work theoretically Quote Link to comment https://forums.phpfreaks.com/topic/155756-listing-all-filesdirectories-via-a-page/#findComment-819866 Share on other sites More sharing options...
stickynote427 Posted April 28, 2009 Author Share Posted April 28, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/155756-listing-all-filesdirectories-via-a-page/#findComment-821442 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.