Jump to content

[SOLVED] Display all images from a directory?


tagger

Recommended Posts

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!

Link to comment
Share on other sites

As simple as it gets - glob() is explained in the manual. Add this to your main folder (as index.php if you want).

<?php
foreach (glob("images/*.jpg") as $filename) {
    echo "<img src='" . $filename . "'><br/>";
}
?> 

Link to comment
Share on other sites

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:

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

 

 

Link to comment
Share on other sites

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)

 

<?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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.