0

I have a folder named hemlata993. So the structure of the folder is:

hemlata993 - It has 4 folder named as folder1, folder2, folder3, folder4 Inside folder1 there are a couple of images and same goes for rest of the folders.

hemlata993>folder1>image1, image2 etc. hemlata993>folder2>image22, image44 etc. and so on..

My code:

$image = './userPosts/hemlata993';


if (is_dir($image)) {
    if ($dh = opendir($image)) {
      while (($file = readdir($dh)) !== false) {
          echo $file. "</br>"; 
  $imageInDir = $image.'/'.$file;
 
  echo $imageInDir. "</br>";
           
$files = glob($imageInDir."*.*");
for ($i = 0; $i < count($files); $i++) {
    $userImage = $files[$i];
  $fin = $userImage[1];
   echo $fin;
 
    echo '<img src="'.$fin.'" width="100" height="100" alt="Random image" />' . "<br /><br />";

}
  
    }
}
}

The output:

folder3
./userPosts/hemlata993/folder3
folder2
./userPosts/hemlata993/folder2
folder4
./userPosts/hemlata993/folder4
..
./userPosts/hemlata993/..
.
./userPosts/hemlata993/.
/Random image

folder1
./userPosts/hemlata993/folder1

Not sure what is this in the middle of output as it does not show any folder name at the end:

..
    ./userPosts/hemlata993/..
    .
    ./userPosts/hemlata993/.

I am not sure but it is not printing the images inside the folder. How can I make it work? Also, where it says /Random image that's the only image it displays and that's also without the picture (more like a square box that says alt value in it). Any help is appreciated of how to print all the images inside hierarchy of folders?

iachi
  • 115
  • 2
  • 11
  • Does this answer your question? [Regarding the Single and the Double Dot within Directories](https://stackoverflow.com/questions/3479744/regarding-the-single-and-the-double-dot-within-directories) – FluffyKitten Aug 02 '20 at 04:27
  • @FluffyKitten Yes, it tells me a difference between dots, but my actual question is how to print all the files in the directory? I am not able to show any images inside hierarchy of folders as stated in my question. – iachi Aug 02 '20 at 04:29
  • You seem to be asking multiple things in your question. Each quection here should be about just one single problem. The link below answers the first question, if you still have another problem or query you would like help with, you can ask another question about it. – FluffyKitten Aug 02 '20 at 04:29
  • @FluffyKitten I understand what you are saying, but it's the part of my actual question it's why I am asking in the same question. As I don't want it to print those dots rather just show the folder1 directory at the bottom right away, so I once I get the correct directory I can think of making it work – iachi Aug 02 '20 at 04:30
  • You are asking **1.** why there are no folder names at the end of `..` etc (which is answered in the link above) and **2.** why is it not printing the images. That's a separate question, but it has actually been asked here before so there is no need to open another question (it will just get closed as a duplicate). The answers here should help you: [Listing all the folders subfolders and files in a directory using php](https://stackoverflow.com/questions/7121479/), or [List all the files and folders in a Directory with PHP recursive function](https://stackoverflow.com/questions/24783862/) – FluffyKitten Aug 02 '20 at 04:45

0 Answers0