html - Reading file directly from folder in php

I want my code to read the file(image) from directly from the folder. But only one file(image) it read and still reading it again (infinity) it didn't stop display one same file(image). But not all the other five different files (image).
<?php
$dir_path = "image/gundam/";
$extensions_array = array('jpg','png','jpeg');
if(is_dir($dir_path)){
$files = scandir($dir_path);
for($i = 0; $i < count($files); $i++){
while($files[$i] !='.' && $files[$i] !='..'){
// get file name
echo "File Name -> $files[$i]<br>";
// get file extension
$file = pathinfo($files[$i]);
$extension = $file['extension'];
echo "File Extension-> $extension<br>";
// check file extension
if(in_array($extension, $extensions_array)){
// show image
echo "<img src='$dir_path$files[$i]' style='width:100px;height:100px;'><br>";
}
}
}
}
?>
Answer
Solution:
This works for me , i tested locally, You don't need to for loop to do this
$dir = 'var/images/';
$extensions = array('jpg', 'jpeg', 'png', 'gif');
if( false !== is_dir($dir) ) {
$dir_contents = scandir( $dir );
foreach($dir_contents as $images) {
$file_ext = pathinfo($images, PATHINFO_EXTENSION);
if( ($images !== '.') &&
($images !== '..') &&
(in_array($file_ext,$extensions))
) {
echo "<img src='$dir$images' style='width:100px;height:100px;'><br>";
}
}
} else {
echo "Path is not directory";
}
Answer
Solution:
To be very specific to your problem, You could useGLOB
to find out the files in the folder and then match them against your extension list and then display the images. you could use the following code to display all images with specific extension in a given folder.
$FILES = glob($PATH."/*.*");
for ($i=0; $i<count($FILES); $i++) {
$fileName = $FILES[$i];
$fileExtension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
if (in_array($fileExtension, $ALLOWEDTYPES)) {
echo "<img style='margin: 10px auto' height='150' width='150' src='".$fileName ."' alt='".$fileName."' />";
} else {
continue;
}
}
generally speaking, you could also use a scandir as the other user mentioned in post above to achieve similar result to display all the images from a given folder.
$FILES = scandir($PATH);
foreach ($FILES as $index => $imageUrl) {
echo "<img style='margin: 10px auto' height='150' width='150' src='".$PATH."/".$imageUrl."' alt='".$imageUrl."' />";
}
I have a working example hosted on phpsandbox here: https://phpsandbox.io/n/lucky-pond-poov
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the metadata storage is not up to date, please run the sync-metadata-storage command to fix this issue.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.