Jump to content

Image Listing/File Listing

- - - - -

  • Please log in to reply
No replies to this topic

#1
brokenbylaw

brokenbylaw

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
This is not going to be the best tutorial, I found the need to easily show all of the images in a dir, so I wrote this quick little guy!



<?php
$dir = "jb/";
$file_array = scandir($dir);

foreach ($file_array as $file){
    echo $file . "<br />";
}
?>
so this will list every single file in the DIR, if you have noticed there are 2 extra little guys in there, the dots, those are directory placements, .. means up a dir and . means current dir.

so how do we get rid of them? If we try to replace the dots with a null character or a space that screws up the .jpg or .html

<?php
$dir = "jb/";
$file_array = scandir($dir);

foreach ($file_array as $file){
$file = $dir . $file;
    if(is_file($file)){
        echo $file . "<br />";
    }
}
?>
we will concatenate $dir to $file, because $file alone will not output the actual file location, you can later strip the $dir for output but i left it so i could use it as a
<img src="">
deal :)

hopefully this wasn't a waste of space, and it helps some people out!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users