forumnz Posted April 6, 2007 Share Posted April 6, 2007 How can i display all the images in a certain directory and echo the image names. So if a new image is uploaded, it will still display without edited the gallery.php file. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/45942-display-all-images-in-a-directory/ Share on other sites More sharing options...
per1os Posted April 6, 2007 Share Posted April 6, 2007 http://us3.php.net/manual/en/class.dir.php read up. Quote Link to comment https://forums.phpfreaks.com/topic/45942-display-all-images-in-a-directory/#findComment-223167 Share on other sites More sharing options...
forumnz Posted April 6, 2007 Author Share Posted April 6, 2007 Thanks.. I have come up with this but it wont display the image. What is wrong with it? <?php $d = dir("upload/sport_motocross"); echo "Handle: " . $d->handle . "\n"; echo "Path: " . $d->path . "\n"; while (false !== ($entry = $d->read())) { echo "<a img src=" . $entry."\n" . "/>"; } $d->close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/45942-display-all-images-in-a-directory/#findComment-223181 Share on other sites More sharing options...
per1os Posted April 6, 2007 Share Posted April 6, 2007 <a img src Basic html, the a tag is a hyperlink, you need it to be just <img src= Also the location as to be the absolute path meaning http://www.site.com/dir/pic.jpg because just pic.jpg won't work Quote Link to comment https://forums.phpfreaks.com/topic/45942-display-all-images-in-a-directory/#findComment-223184 Share on other sites More sharing options...
forumnz Posted April 6, 2007 Author Share Posted April 6, 2007 Right ok. Now it works but it also tries to displays images for the two directories - even though they aren't images. How can I stop this? Quote Link to comment https://forums.phpfreaks.com/topic/45942-display-all-images-in-a-directory/#findComment-223194 Share on other sites More sharing options...
per1os Posted April 6, 2007 Share Posted April 6, 2007 Do an if statement and check the filename for .jpg, .gif, .png, .jpeg if it is not that extension don't add it. Quote Link to comment https://forums.phpfreaks.com/topic/45942-display-all-images-in-a-directory/#findComment-223207 Share on other sites More sharing options...
forumnz Posted April 7, 2007 Author Share Posted April 7, 2007 I cant do that - I don't know how :-\ Heres the link - can ya help, anyone? http://designervision.co.nz/test/view.php Quote Link to comment https://forums.phpfreaks.com/topic/45942-display-all-images-in-a-directory/#findComment-223383 Share on other sites More sharing options...
per1os Posted April 7, 2007 Share Posted April 7, 2007 <?php $d = dir("upload/sport_motocross"); echo "Handle: " . $d->handle . "\n"; echo "Path: " . $d->path . "\n"; while (false !== ($entry = $d->read())) { if ($entry == "." || $entry == "..") { continue; }elseif (!ereg('gif|jpg|jpeg|png', $entry)) { continue; } echo "<img src=" . $entry."\n" . "/>"; } $d->close(); ?> Try that. Quote Link to comment https://forums.phpfreaks.com/topic/45942-display-all-images-in-a-directory/#findComment-223388 Share on other sites More sharing options...
forumnz Posted April 7, 2007 Author Share Posted April 7, 2007 Excellent, thanks mate! Quote Link to comment https://forums.phpfreaks.com/topic/45942-display-all-images-in-a-directory/#findComment-223391 Share on other sites More sharing options...
kenrbnsn Posted April 7, 2007 Share Posted April 7, 2007 I find it's much easier to use the glob() function. <?php fioreach(glob({'upload/sport_motocross/*.jpg','upload/sport_motocross/*.gif','upload/sport_motocross/*.png'}, GLOB_BRACE) as $file) { list($h, $w, $dmy1, $attr, $dmy2) = getimagesize($file); echo '<img src="' . $file . '" ' . $attr . "/><br />\n"; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/45942-display-all-images-in-a-directory/#findComment-223397 Share on other sites More sharing options...
forumnz Posted April 7, 2007 Author Share Posted April 7, 2007 Ok I have one more problem. How can I get the images to display in rows of 5 instead of one above the other? This is my code so far. <?php $d = dir("sport_motocross"); //echo "Handle: " . $d->handle . "\n"; //echo "Path: " . $d->path . "\n"; while (false !== ($entry = $d->read())) { if ($entry == "." || $entry == "..") { continue; }elseif (!ereg('gif|jpg|jpeg|png', $entry)) { continue; } echo "<img src=" . "http://www.designervision.co.nz/magik/gallery/sport_motocross/" .$entry. "\n" . "/>" . "<br />" . $entry. "<br />"; } $d->close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/45942-display-all-images-in-a-directory/#findComment-223408 Share on other sites More sharing options...
The Little Guy Posted April 7, 2007 Share Posted April 7, 2007 <?php foreach(glob("/path/to/directory/*.jpg") as $img){ echo $img.'<br>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/45942-display-all-images-in-a-directory/#findComment-223410 Share on other sites More sharing options...
kenrbnsn Posted April 7, 2007 Share Posted April 7, 2007 You either have to put the images in a table or use floating <divs>. Ken Quote Link to comment https://forums.phpfreaks.com/topic/45942-display-all-images-in-a-directory/#findComment-223412 Share on other sites More sharing options...
forumnz Posted April 7, 2007 Author Share Posted April 7, 2007 I hate to ask, but how? Quote Link to comment https://forums.phpfreaks.com/topic/45942-display-all-images-in-a-directory/#findComment-223415 Share on other sites More sharing options...
dink87522 Posted November 28, 2010 Share Posted November 28, 2010 Thanks, this was very helpful, exactly what I was looking for Quote Link to comment https://forums.phpfreaks.com/topic/45942-display-all-images-in-a-directory/#findComment-1140575 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.