Rafael Tomé
Rafael Tomé

Reputation: 15

Load all images from folder

I'm trying to insert images from a folder into a gallery made in javascript.

I'm using a php cycle to insert all the images but it doesn't work. Here is the code:

<div id="mygallery">
            <?php $directory="img/portfolio";
            $images=glob($directory . "*.jpg");
            $swipebox = "swipebox";
            foreach($images as $image) {
                echo "<a href=" .$image." class=".$swipebox."> <img alt=" . $directory . "src=". $image . "/>";
            }?></div>

Upvotes: 1

Views: 3146

Answers (4)

Elixir Techne
Elixir Techne

Reputation: 1856

Above comments have mentioned two things. 1. Add slash (/) in path 2. Add Space between src and alt.

The list retuned by glob function is filenames/directory names matching given pattern. you have to join path in image source. Try below code. It should work.

<div id="mygallery">
<?php
    $directory="img/portfolio/";
    $images=glob($directory . "*.jpg");
    $swipebox = "swipebox";
    foreach($images as $image) {
        echo "<a href=" .$image." class=".$swipebox."> <img alt=" . $directory . " src=". $directory .$image . " /></a>";
    }
?>
</div>

Upvotes: 0

Debesh Nayak
Debesh Nayak

Reputation: 244

Here is the correct code:

<div id="mygallery">
<?php
    $directory="img/portfolio/";
    $images=glob($directory . "*.jpg");
    $swipebox = "swipebox";
    foreach($images as $image) {
        echo "<a href=" .$image." class=".$swipebox."> <img alt=" . $directory . " src=". $image . " /></a>";
    }
?>
</div>

Here are the mistake that you have done in your code:
1. Need to put slash '/' at after line no 3 after the image directory path
2. Need to put space between src and alt attribute at line no 7

Upvotes: 1

Anto S
Anto S

Reputation: 2449

Try below,

<?php
    $allImageFiles = scandir('img/portfolio');
    $swipebox = "swipebox";


    foreach($allImageFiles as $key=>$image) {
        echo "<a href=" .$image." class=".$swipebox."> <img alt=" . $directory . "src=". $image . "/>";

    }
?>

Upvotes: 0

Eduardo Salazar
Eduardo Salazar

Reputation: 187

This is working, you needed a space before "src" and close the href.

<?php 
    $directory="img/portfolio";
    $images=glob($directory . "*.jpg");
    $swipebox = "swipebox";
    foreach($images as $image) {
        echo " <a href=" .$image." class=".$swipebox."> <img alt=" . $directory . " src='". $image . "' /> </a>";
}?>

Upvotes: 0

Related Questions