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
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>
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>
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);
}
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>";
}
}
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.
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>