Oikofugic Posted June 8, 2019 Share Posted June 8, 2019 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted June 8, 2019 Share Posted June 8, 2019 I'm guessing that the two broken ones are the filenames "." and ".." Test for these and ignore them. Quote Link to comment Share on other sites More sharing options...
Oikofugic Posted June 8, 2019 Author Share Posted June 8, 2019 Thanks for your reply, but there is only one image in the folder and nothing else. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 8, 2019 Share Posted June 8, 2019 No, there is more, you just don't understand file systems. is_file Quote Link to comment Share on other sites More sharing options...
Oikofugic Posted June 8, 2019 Author Share Posted June 8, 2019 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"/>'; } } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted June 8, 2019 Share Posted June 8, 2019 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\"/>"; } Quote Link to comment Share on other sites More sharing options...
Oikofugic Posted June 8, 2019 Author Share Posted June 8, 2019 Hi Barand, Have tried with your suggestion, no joy unfortunately, undoubtedly my lack of understanding, but thanks anyway.. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 8, 2019 Share Posted June 8, 2019 Why dont' you show us what your code looks like now so we can help you? Quote Link to comment Share on other sites More sharing options...
Oikofugic Posted June 8, 2019 Author Share Posted June 8, 2019 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted June 8, 2019 Share Posted June 8, 2019 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); } ?> Quote Link to comment Share on other sites More sharing options...
Oikofugic Posted June 8, 2019 Author Share Posted June 8, 2019 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); } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.