Jump to content

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

 

 

 

 

Share this post


Link to post
Share on other sites

I'm guessing that the two broken ones are the filenames "." and ".."

Test for these and ignore them.

Share this post


Link to post
Share on other sites

Thanks for your reply, but there is only one image in the folder and nothing else.

Share this post


Link to post
Share on other sites

No, there is more, you just don't understand file systems.

is_file

Share this post


Link to post
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"/>';
    }
}

?>

 

Share this post


Link to post
Share on other sites

or you could have RTFM using the link @requinix gave you then

    if (is_file($dir.$file)) {
        echo "<img src=\"$dir$file\" width=\"120px\" height=\"120px\"/>";
    }

 

Share this post


Link to post
Share on other sites

Hi Barand,

Have tried with your suggestion, no joy unfortunately, undoubtedly my lack of understanding, but thanks anyway..

Share this post


Link to post
Share on other sites

Why dont' you show us what your code looks like now so we can help you?

Share this post


Link to post
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

Share this post


Link to post
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);
}
?>

 

Share this post


Link to post
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);
  }
}
 
?>

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.