4

I am wanting to find out if the following is possible with PHP inside WordPress.

Basically if I have a directory in my site called "promos" that consists of 1 to many images (as these images can change), that I would like to read from within a PHP file into a carousel setup, i.e. something similar to this:

   <div class="scrollable" id="browsable">   

   <div class="items"> 

          <?php 
            $tot_images_from_promo_dir = [get_total_image_count_in_promos_dir]; 

            for ( $counter = 1; $counter <= $tot_images_from_promo_dir; $counter ++) {
                echo "<div>";
                    echo "<a href="#"><img src="[image_from_promo_directory]" /></a>
                echo "</div>";
            }
          ?>
    </div>
</div>

Basically want to somehow with php read total amount of images in my promo directory and then use this total in my loop max value and read each image file name in the promo directory and pass into my <img src=[image_name_from_promo_dir].

halfer
  • 19,824
  • 17
  • 99
  • 186
tonyf
  • 34,479
  • 49
  • 157
  • 246
  • This can definitely be done. Are there any files other than the images in your promos directory? – Jeffrey Blake Aug 22 '10 at 03:35
  • No. Will only consist of images. – tonyf Aug 22 '10 at 11:21
  • I answered a very similar question here: http://stackoverflow.com/questions/3275718/is-there-a-way-to-put-this-php-into-an-array-and-simplify-it/3278243#3278243 It should be what you need. – Gipetto Aug 23 '10 at 14:13

6 Answers6

12

Assuming that all files in the promos directory are images:

<div class="scrollable" id="browsable">   
   <div class="items"> 
      <?php 
        if ($handle = opendir('./promos/')) {

            while (false !== ($file = readdir($handle))) {
                echo "<div>";
                    echo "<a href='#'><img src='".$file."' /></a>";
                echo "</div>";
            }
            closedir($handle);
        }
      ?>
    </div>
</div>

If, however, there are files in the directory that are not images, you would need to check that before showing it. The while loop would need to change to something like:

while (false !== ($file = readdir($handle))) {
    if ((strpos($file, ".jpg")) || (strpos($file, ".gif"))) {
        echo "<div>";
            echo "<a href='#'><img src='".$file."' /></a>";
        echo "</div>";
    }
}
Jeffrey Blake
  • 9,659
  • 6
  • 43
  • 65
1

My way to do it with opendir

<div class="scrollable" id="browsable">   
<div class="items"> 

<?php
    $c=0;
    if ($handle = opendir('promo')) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                $images[$c] = $file;
                $c++;
            }
        }
        closedir($handle);
    }

    for ($counter = 0; $counter <= count($images); $counter ++) {
        echo "<div>";
            echo '<a href="#"><img src="'.$image[$counter].'" /></a>';
        echo "</div>";
    }
          ?>
    </div>
</div>
Mark Lalor
  • 7,820
  • 18
  • 67
  • 106
1

I use glob for such operations

<div class="scrollable" id="browsable">   

   <div class="items"> 

          <?php 
            $images = glob("path_to_folder/*.{gif,jpg,png}", GLOB_BRACE);

            for ( $counter = 1; $counter < sizeof($images); $counter ++) {
                echo "<div>";
                    echo '<a href='#'><img src=".$images[$counter]." /></a>';
                echo "</div>";
            }
          ?>
    </div>
</div>
0

This code will provides the images from the directory called "imagfolder"

if($handle = opendir(dirname(realpath(__FILE__)).'/imagefolder/')){
                while($file = readdir($handle)){
                    if($file !== '.' && $file !== '..')
                    {
                        echo '<div id="images">';
                        echo '<img src="imagefolder/'.$file.'" border="0" />';
                        echo '</div>';
                    }
                }
                closedir($handle);
        }
Naveenbos
  • 2,532
  • 3
  • 34
  • 60
0

Go thou unto http://www.php.net/manual/en/ref.dir.php and look in particular at the scandir function. You can use something like:

$images = array();
foreach (scandir('somewhere') as $filename)
    if (is_an_image($filename)) $images[] = $filename;

You get to write the is_an_image() function.

Ian
  • 4,421
  • 1
  • 20
  • 17
0

How about the DirectoryIterator class?

<div class="scrollable" id="browsable">   
    <div class="items"> 
        <?php foreach (new DirectoryIterator('folder/goes/here') as $file): ?>
        <?php if($file->isDot || !$file->isReadable()) continue; ?>

            <div><a href="#"><img src="filepath/<?php echo $file->getFilename(); ?>" /></a></div>

          <?php endforeach; ?>
    </div>
</div>
Kieran Allen
  • 961
  • 5
  • 6
  • Also using iterators, have a look at this question: http://stackoverflow.com/questions/3321547/help-using-regexiterator-in-php – Chris Aug 22 '10 at 15:34