jderosa3 Posted April 30, 2009 Share Posted April 30, 2009 First off I am using exec-php with Wordpress to execute PHP code in my "static" pages - I assume what I want to do it simple, but I am having issues. What I want to do is just display all the files (JPGs) from a specified directory. I coded the following: <?php $dir = "./flash/portfolio/branding/images/"; //open dir if ($opendir = opendir($dir)) { //read dir while (($file = readdir($opendir)) !== FALSE) { if ($file!="."&&$file!="..") echo $file."<img src='$dir/$file'><br />"; } } ?> but... all this does is list the files names and right next to them it displays a broken icon as if the graphics are not there... what gives... can any one test this... and let me know their results... or is there another method to display graphics in this manner. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/156206-solved-displaying-images-from-a-directory/ Share on other sites More sharing options...
Ken2k7 Posted April 30, 2009 Share Posted April 30, 2009 if ($file!="."&&$file!="..") See anything wrong with that? Quote Link to comment https://forums.phpfreaks.com/topic/156206-solved-displaying-images-from-a-directory/#findComment-822337 Share on other sites More sharing options...
jderosa3 Posted April 30, 2009 Author Share Posted April 30, 2009 honestly I got this code by following a tutorial on you tube... think you can help me out? I believe you are trying to give me a hint and I wish I knew what it was... ??? Quote Link to comment https://forums.phpfreaks.com/topic/156206-solved-displaying-images-from-a-directory/#findComment-822341 Share on other sites More sharing options...
Ken2k7 Posted April 30, 2009 Share Posted April 30, 2009 Try this: <?php $dir = "./flash/portfolio/branding/images/"; //open dir if ($opendir = opendir($dir)) { //read dir while (($file = readdir($opendir)) !== FALSE) { if ($file!="."&&$file!="..") echo $file."<img src='$dir/$file'><br />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156206-solved-displaying-images-from-a-directory/#findComment-822347 Share on other sites More sharing options...
premiso Posted April 30, 2009 Share Posted April 30, 2009 If all you are looking for is a file name. Try glob. $jpgs = glob($dir . "*.JPG"); foreach ($jpgs as $jpg) { echo "<img src='{$jpg}' /><br />"; } A bit less code and easier, in my opinion. As far as it showing an x, the path needs to be relevant to the actual web path not the server path. So make sure that is what it is doing. Quote Link to comment https://forums.phpfreaks.com/topic/156206-solved-displaying-images-from-a-directory/#findComment-822351 Share on other sites More sharing options...
jderosa3 Posted April 30, 2009 Author Share Posted April 30, 2009 Try this: <?php $dir = "./flash/portfolio/branding/images/"; //open dir if ($opendir = opendir($dir)) { //read dir while (($file = readdir($opendir)) !== FALSE) { if ($file!="."&&$file!="..") echo $file."<img src='$dir/$file'><br />"; } } ?> My bad... that was how my code was... it must have converted the && to && when i pasted it in... it still gives me those broken image icons... my question is... how can it list the correct files names of the directory and not the image itself... it must be reading the directory correctly if it can list the files, correct? Quote Link to comment https://forums.phpfreaks.com/topic/156206-solved-displaying-images-from-a-directory/#findComment-822367 Share on other sites More sharing options...
jderosa3 Posted April 30, 2009 Author Share Posted April 30, 2009 If all you are looking for is a file name. Try glob. $jpgs = glob($dir . "*.JPG"); foreach ($jpgs as $jpg) { echo "<img src='{$jpg}' /><br />"; } A bit less code and easier, in my opinion. As far as it showing an x, the path needs to be relevant to the actual web path not the server path. So make sure that is what it is doing. So would i in essence put this in my page like this: <?php $jpgs = glob($dir ./flash/portfolio/branding/images/ "*.JPG"); foreach ($jpgs as $jpg) { echo "<img src='{$jpg}' /><br />"; } ?> Im sorry I have been so rusty with this stuff... Quote Link to comment https://forums.phpfreaks.com/topic/156206-solved-displaying-images-from-a-directory/#findComment-822368 Share on other sites More sharing options...
premiso Posted April 30, 2009 Share Posted April 30, 2009 mmm no. $dir = "./flash/portfolio/branding/images/"; $jpgs = glob($dir . "*.JPG"); foreach ($jpgs as $jpg) { echo "<img src='{$jpg}' /><br />"; } But that will only pull JPG that end in all caps. So if the .jpg is all caps or all lowercase modify accordingly. Quote Link to comment https://forums.phpfreaks.com/topic/156206-solved-displaying-images-from-a-directory/#findComment-822369 Share on other sites More sharing options...
jderosa3 Posted April 30, 2009 Author Share Posted April 30, 2009 OK... I used as you said... <?php $dir = "./flash/portfolio/branding/images/"; $jpgs = glob($dir . "*.jpg"); foreach ($jpgs as $jpg) { echo "<img src='{$jpg}' /><br />"; } ?> I know that is grabbing the correct amount of files.. in my case... 5... but they all have broken image icons.. so obviously this is working correctly... but it must be something in wordpress causing this... I just can't understand... especially because it is grabbing the correct number of files... I will look further into this... thanks for the help so far... I truly appreciate it... Quote Link to comment https://forums.phpfreaks.com/topic/156206-solved-displaying-images-from-a-directory/#findComment-822379 Share on other sites More sharing options...
xtopolis Posted April 30, 2009 Share Posted April 30, 2009 What about echo "<img src='" . $dir . $jpg . "' /><br />"; ? Is it because the images are not mapping to the correct path (just $jpg) in your code? Quote Link to comment https://forums.phpfreaks.com/topic/156206-solved-displaying-images-from-a-directory/#findComment-822391 Share on other sites More sharing options...
jderosa3 Posted April 30, 2009 Author Share Posted April 30, 2009 I am not sure what is going on... I am assuming it is wordpress... try doing a sample installation and running the exec-php plugin... When I check the properties on the broken image icons... I get the following code: <img src="./flash/portfolio/branding/images/philco-sticker.jpg"> the flash directory is in the root of my site... for example: http://www.xyz.com/flash/portfolio/branding/images/philco-sticker.jpg I would rule out that I am doing something wrong if someone else was able to try this and produce the same results as me... driving me nuts... Quote Link to comment https://forums.phpfreaks.com/topic/156206-solved-displaying-images-from-a-directory/#findComment-822392 Share on other sites More sharing options...
premiso Posted April 30, 2009 Share Posted April 30, 2009 Why do you have the initial . ??? Try removing that and see what happens. @xtopolis glob carries the directory path with it, if it was specified in the search terms. Quote Link to comment https://forums.phpfreaks.com/topic/156206-solved-displaying-images-from-a-directory/#findComment-822394 Share on other sites More sharing options...
jderosa3 Posted April 30, 2009 Author Share Posted April 30, 2009 What about echo "<img src='" . $dir . $jpg . "' /><br />"; ? Is it because the images are not mapping to the correct path (just $jpg) in your code? I tried this as well... same results... broken images... Quote Link to comment https://forums.phpfreaks.com/topic/156206-solved-displaying-images-from-a-directory/#findComment-822397 Share on other sites More sharing options...
premiso Posted April 30, 2009 Share Posted April 30, 2009 What happens if you add the http://www.xyz.com onto the path? echo "<img src='http://www.xyz.com{$jpg}' /><br />"; If that does not work try this to remove the initial dot: $jpg = substr($jpg, 1); echo "<img src='http://www.xyz.com{$jpg}' /><br />"; And see if that makes the image display. Quote Link to comment https://forums.phpfreaks.com/topic/156206-solved-displaying-images-from-a-directory/#findComment-822399 Share on other sites More sharing options...
jderosa3 Posted April 30, 2009 Author Share Posted April 30, 2009 Why do you have the initial . ??? Try removing that and see what happens. @xtopolis glob carries the directory path with it, if it was specified in the search terms. I tried it and that doesn't work... nothing shows up at all.. I believe I need that "." because that throws me back to the root of the site. Wordpress works within side of it's wp-content directory... so in order for me to access something in the root of my site... I assume I need that beginning "." Quote Link to comment https://forums.phpfreaks.com/topic/156206-solved-displaying-images-from-a-directory/#findComment-822400 Share on other sites More sharing options...
jderosa3 Posted April 30, 2009 Author Share Posted April 30, 2009 ;D ;D This did it!!!! <?php $dir = "./flash/portfolio/branding/images/"; $jpgs = glob($dir . "*.jpg"); foreach ($jpgs as $jpg) { $jpg = substr($jpg, 1); echo "<img src='http://www.xyz.com{$jpg}' /><br />"; } ?> why this worked this way is beyond me! Thank you very very much!!!! No wonder this is the PHP freaks forum... you guys (and gals) rock! Quote Link to comment https://forums.phpfreaks.com/topic/156206-solved-displaying-images-from-a-directory/#findComment-822401 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.