Author Topic: [SOLVED] Display all images from a directory?  (Read 565 times)

0 Members and 1 Guest are viewing this topic.

Offline taggerTopic starter

  • Irregular
  • Posts: 2
    • View Profile
[SOLVED] Display all images from a directory?
« on: August 11, 2007, 02:09:45 PM »
Hi, I'm new to php and hope to find some help here. I searched for this but couldn't find anything.
I know HTML and CSS very well.

I'm a lazy guy who has to upload a fair amount of pictures and show them online.
To make the story short: I'm an artist who tries to upload every sketchbook page to show others.
Basically I would like to have 1 directory where I could upload images (jpg format) and the script searches through that directory and displays all the images with <img> tags.

I would like to have the index.php in the root directory and the images in a folder called 'images'.

I would be very glad if somebody could code something for me and gve a small explanation.
I made a script which didn't 'really' worked out because it did display the images but also other weird stuff that I don't understand.

Thanks in advance!

Offline AndyB

  • back from the North Pole ... and retired!
  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,521
  • php 5.2.3 / MySQL 5.0.27
    • View Profile
    • halfadot smallwebs
Re: Display all images from a directory?
« Reply #1 on: August 11, 2007, 02:46:23 PM »
As simple as it gets - glob() is explained in the manual. Add this to your main folder (as index.php if you want).
Code: [Select]
<?php
foreach (glob("images/*.jpg") as $filename) {
    echo 
"<img src='" $filename "'><br/>";
}
?>
Legend has it that reading the manual never killed anyone.
My site

Offline taggerTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Re: Display all images from a directory?
« Reply #2 on: August 11, 2007, 03:16:54 PM »
Works perfectly, thanks a lot! :D

I have an additional question, it would be great if you could answer this too:

When I want to apply a style or use a class tag with the image can I just do it like this:
Code: [Select]
<?php
foreach (glob("images/*.jpg") as $filename) {
    echo 
"<img class="sketchbookimages" src='" $filename "'><br/>";
}
?>



Offline calabiyau

  • Enthusiast
  • Posts: 271
    • View Profile
Re: Display all images from a directory?
« Reply #3 on: August 11, 2007, 03:23:55 PM »
Like below:

Code: [Select]
<?php
foreach (glob("images/*.jpg") as $filename) {
    echo 
'<img class="sketchbookimages" src="' $filename '"><br/>';
}
?>



[/quote]

Offline LiamProductions

  • Enthusiast
  • Posts: 498
    • View Profile
Re: Display all images from a directory?
« Reply #4 on: August 11, 2007, 03:26:31 PM »
Even though this isnt my topic..  Thanks. I've learnt a new syntax  :P
Quote
In this hour, Your going to get your hands dirty with some of the nuts and bolts in the language

Offline LiamProductions

  • Enthusiast
  • Posts: 498
    • View Profile
Re: Display all images from a directory?
« Reply #5 on: August 11, 2007, 03:38:12 PM »
Thanks to this i made a script (I dont achually need it but i still made it i still had problems lol but i fixed them)

Code: [Select]
<?php
error_reporting
(E_ALL);
foreach(
glob("*.php") as $filename){
  echo 
"<a href=\"$filename\">$filename</a> <br />";
  }
?>

it will get all the file names with .php and link to the file name :) like a directory kinda :)
Quote
In this hour, Your going to get your hands dirty with some of the nuts and bolts in the language