Read images from a directory using PHP

后端 未结 6 526
广开言路
广开言路 2020-12-19 16:35

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

相关标签:
6条回答
  • 2020-12-19 16:44

    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>
    
    0 讨论(0)
  • 2020-12-19 16:57

    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>
    
    0 讨论(0)
  • 2020-12-19 16:58

    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);
            }
    
    0 讨论(0)
  • 2020-12-19 17:05

    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>";
        }
    }
    
    0 讨论(0)
  • 2020-12-19 17:07

    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.

    0 讨论(0)
  • 2020-12-19 17:07

    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 讨论(0)
提交回复
热议问题