Read Images from a Directory using PHP

2019-01-26 16:17发布

Is this 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>

Hope the above makes sense, but 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

Would really appreciate the php syntax to do this.

Thanks.

标签: php wordpress
6条回答
smile是对你的礼貌
2楼-- · 2019-01-26 16:23

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>
查看更多
淡お忘
3楼-- · 2019-01-26 16:25

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>";
    }
}
查看更多
Lonely孤独者°
4楼-- · 2019-01-26 16:34

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>
查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-26 16:41

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.

查看更多
ら.Afraid
6楼-- · 2019-01-26 16:41

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);
        }
查看更多
爷的心禁止访问
7楼-- · 2019-01-26 16:43

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>
查看更多
登录 后发表回答