Jump to content

Read folder


Oikofugic

Recommended Posts

Hi all,

I'm trying to echo all the files in the folder 'pics'

<?php

$dir = "pics/";

// Open a directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
      echo '<img src="pics/' .$file . '" width="120px" height="120px" />';
    }
    closedir($dh);
  }
}

?>

 

 

The above code outputs the following. (there is only one image in the folder) So the code gets the image correctly and works with multiple images in the folder, but also produces two broken pathways at the beginning.

<img src="pics/." width="120px" height="120px" /><img src="pics/.." width="120px" height="120px" /><img src="pics/j1.jpg" width="120px" height="120px" />

 

My question is why, I can't figure out what is happening. Any pointers or explanations would be appreciated. Cheers for your time.

 

Paul

 

 

 

 

Link to comment
Share on other sites

Thanks requinix,

It's still foo bar tbh, but got it working. Ta

<?php
$dir = "pics/";
$dh = opendir($dir);

while (false !== ($file = readdir($dh))) {
 
    $pos = strrpos($file, '.');
    if (false !== $pos && strlen($file) > $pos + 1) {
        $ext = substr($file, $pos + 1);
echo '<img src="pics/'.$file.'" width="120px" height="120px"/>';
    }
}

?>

 

Link to comment
Share on other sites

Hi ginerjim, as I previously posted I have it working, with help from the link provided by requinix, then Berand popped back with his RTFM which I initially thought was an abbreviation for something useful that I would find on that page. I copied his code and tried to implement it into what I had, assuming it would negate the need to strip out the dots, but ended up with Undefined variable warning.

It might have ultimately been for a more elegant solution Barand suggested, but as I had working anyway, I figured a partial solution with no explanation was not at this time necessary.

This is what I was trying, but as you can no doubt tell I'm getting muddled

<?php
$dir = "pics/";
$file ="";
// Open a directory, and read its contents
if (is_file($dir.$file)) {
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
       echo "<img src=\"$dir$file\" width=\"120px\" height=\"120px\"/>";
    }
    closedir($dh);
  }
}
?>

I have tried various combinations etc

So attempting to defile $file, but not quite sure as to why

Link to comment
Share on other sites

It was something that would be useful to you

When you do a dir listing EG

 Directory of C:\inetpub\wwwroot\test\images

14/03/2019  18:37    <DIR>          .
14/03/2019  18:37    <DIR>          ..
29/04/2012  00:41             1,735 badlogo.PNG
29/04/2012  12:58           233,305 banner.png
29/05/2015  10:32           374,455 cats.jpg
14/05/2012  18:41             1,739 download.png
22/02/2014  20:34            11,907 emplogo.png

the first two names (with the dots) are actually diectories (current and parent). You are only intersted in the files, hence "is_file()" to check each file before you attempt to output it as an image

<?php
$dir = "pics/";
$file ="";
// Open a directory, and read its contents
if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
        if (is_file($dir.$file)) {                                              // <-- it goes here
           echo "<img src=\"$dir$file\" width=\"120px\" height=\"120px\"/>";
        }
    }
    closedir($dh);
}
?>

 

Link to comment
Share on other sites

Thanks for clearing it up for me. I understand now

<?php
$dir = "pics/";

// Open a directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
 if (is_file($dir.$file)) {
      echo '<img src=" '.$dir.$file.' " width="120px" height="120px"/>';
    }

}
    closedir($dh);
  }
}
 
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.