I'm trying to load images from a folder on the local server.
But there are only the 3 images that appear correctly in the folder.
<table class="table">
<?php
$dir = '../images/slideshow';
$file_display = array('jpg', 'jpeg', 'png', 'gif');
if (file_exists($dir) == false) {
echo 'Directory \''. $dir. '\' not found!';
} else {
$dir_contents = scandir($dir);
foreach ($dir_contents as $file) {
$ex = explode('.', $file);
$file_type = strtolower(end($ex));
?>
<tr>
<td><img alt="<?php echo $file?>" width="64px" height="64px" src="<?php echo $dir."/".$file?>">
<td class="td" style="text-align: center;"><a href="<?php echo $_SERVER['PHP_SELF']."?action=delete&id=".$row["id"]; ?>" class="btn btn-default btn-danger" >Delete</a><?php
}
}
?>
</table>
EDIT: I was able to fix this by changing the above code to the following...
<table class="table">
<?php
$dir = '../images/slideshow';
$file_display = array('jpg', 'jpeg', 'png', 'gif');
if (file_exists($dir) == false) {
echo 'Directory \''. $dir. '\' not found!';
} else {
$dir_contents = scandir($dir);
$file_display = array(
'jpg',
'jpeg',
'png',
'gif'
);
foreach ($dir_contents as $file) {
$ex = explode('.', $file);
$file_type = strtolower(end($ex));
if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) {
?>
<tr>
<td><img alt="<?php echo $file?>" width="64px" height="64px" src="<?php echo $dir."/".$file?>">
<td class="td" style="text-align: center;"><a href="<?php echo $_SERVER['PHP_SELF']."?action=delete&id=".$row["id"]; ?>" class="btn btn-default btn-danger" >Delete</a><?php
}
}
}
?>
</table>
Just checking the file extensions.
.
and ..
define the current dir and the parent dir from your current path. scandir
fetches all files and folders in your path, so those are displayed as images as well, since you never check for a valid file extension (defined in your $file_display
). To get rid of .
and ..
you can use
$dir_contents = array_diff(scandir($directory), array('.', '..'))