Script show all the images in a directory

in

View the entire directory's images.
Just a quick script so I can view the entire onion smilies directory
It have no mechanism to filter out anything that is not images, some additional conditions need to be applied.

<?php
	$handle = opendir ('./');
    while (false !== ($file = readdir($handle))) {
        echo '<img src="'.$file.'"/><br />'.$file.'<br />';
    }
?>

small bug

Jonathan Street's picture

You may have noticed already but the first two 'images' don't show up. The first two files are for the current directory and moving up a directory

Something simple like the following should solve this.

<?php
        $handle = opendir ('./');
        while (false !== ($file = readdir($handle))) {
            if($file != "." && $file != ". .") {
                echo '&lt;img src="'.$file.'"/&gt;'.$file.'';
            }
        }
?>

Another improvement

Mgccl's picture

thx :),
Now it will not output the script itself as a image.

<?php
        $handle = opendir ('./');
        while (false !== ($file = readdir($handle))) {
            if($file != "." && $file != ".." && $file != basename(__FILE__)) {
                echo '<img src="'.$file.'"/>'.$file.'';
            }
        }
?>

nice script, thanks. but now

nice script, thanks.

but now can it display the images in order of filename?

Thanks, exactly what I was

Thanks, exactly what I was looking for!

The "Another Improvement" script

Charles's picture

I like this one a lot, but have one question...
how can I modify it so it outputs the file name without the period and file type attached?

for this instance, this

Mgccl's picture

for this instance, this should work... did not try the code but I don't think there is an error.
Replaced the instance of $file with substr($file,0,-4). Assuming the the file type has only 3 letters.

<?php
        $handle = opendir ('./');
        while (false !== ($file = readdir($handle))) {
            if($file != "." && $file != ".." && $file != basename(__FILE__)) {
                echo '<img src="'.$file.'"/>'.substr($file,0,-4).'';
            }
        }
?>

Post new comment

The content of this field is kept private and will not be shown publicly.
If you have a Gravatar account, used to display your avatar.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <span> <fn>
  • Lines and paragraphs break automatically.
  • Textual smileys will be replaced with graphical ones.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.

More information about formatting options

What is 15 + 35?
To combat spam, please solve the math question above.
Honey Pot that kill bots