This script displays the contents of my directory, problem s it displays the strange directorie(., …)
<?php
$dirname = 'images/'.$row['user_id'].'/';
$images = scandir($thumbsdirname);
foreach($images as $curimg){
echo "<a class='fancybox' href='".$dirname.$curimg."' data-fancybox-group='gallery' title='".$row['name']."'><img src='".$dirname.$curimg."' alt='' /></a>\
";
};
?>
How do I change it to only display the image in the directory?
Thx
Got, had to create a array of accepted extensions, then I had to add an if statement to see if I either got a., a … or one other extension.
<?php
$thumbsdirname = 'images/'.$row['user_id'].'/thumbs/';
$dirname = 'images/'.$row['user_id'].'/';
$file_types = array ('jpg', 'jpeg', 'png', 'gif');
$images = scandir($thumbsdirname);
foreach($images as $curimg){
$file_type = strtolower(end(explode('.', $curimg)));
if ($curimg !=='.' && $curimg !== '..' && in_array($file_type, $file_types) == true)
{
echo ...
}
};
?>
A new way:
<?php
$directory = new DirectoryIterator(__DIR__);
$file_types = array ('jpg', 'jpeg', 'png', 'gif');
foreach ($directory as $fileinfo) {
if ($fileinfo->isFile() && in_array($fileinfo->getExtension(), $file_types) ) {
echo $fileinfo->getFilename() . "\
";
}
}
?>