Reading images from a directory using PHP

Is this possible with PHP inside WordPress.

Basically, if I have a directory on my site called "promos", consisting of 1 - many images (since these images may change) that I would like to read from the PHP file to the carousel setting, then there is 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> 

I hope the above makes sense, but basically wants to somehow read the total number of images in my promo directory with php, and then use this value in the maximum value of the loop and read each image file name in the promo directory and go to mine

Really appreciate the php syntax for this.

Thank.

+4
php wordpress
Aug 22 '10 at 3:20
source share
6 answers

Assuming all the 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 will need to check this before displaying it. The while loop should 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>"; } } 
+11
Aug 22 2018-10-10T00:
source share

My way to do this 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> 
+1
Aug 22 2018-10-10T00:
source share

Go to 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 can write the is_an_image () function.

0
Aug 22 2018-10-10T00:
source share

What 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> 
0
Aug 22 '10 at 15:31
source share

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
Oct. 15 '11 at 15:43
source share

This code will provide images from a 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); } 
0
Nov 19 '13 at 12:43 on
source share



All Articles