dimi Posted February 12, 2011 Share Posted February 12, 2011 Hi I tried to write a script: 1) to locate all directories in a directory (1 level) 2) with the function GetImages() I try to display the image(s) in the subfolder there are only images in the subfolder I guess I'm doing something wrong in the GetImages() with the glob function, can anyone check this ? Thanks in advance function GetImages($map) { $files = glob('$map/*.jpg'); //$files = glob("$map/*.*"); for ($i=0; $i<count($files); $i++) { $num = $files[$i]; echo '<img height="50" width="50" src="'.$num.'" />'." <br />"; } } if ($handle = opendir('mystuff')) { /* loop through directory. */ while (false !== ($dir = readdir($handle))) { if($dir != ".." && $dir != "."){ echo '<option value='.$dir.'>'.$dir.'</option><br>'; GetImages($dir); } } closedir($handle); } Quote Link to comment https://forums.phpfreaks.com/topic/227457-display-images-in-subfolders/ Share on other sites More sharing options...
denno020 Posted February 12, 2011 Share Posted February 12, 2011 Your for loop never runs as count($files) returns 0. I'm not sure why, but I thought I would let you know if you didn't already do you could work something else out. I'm going to keep looking at it myself too as I'm interested to see how it's done. Denno Quote Link to comment https://forums.phpfreaks.com/topic/227457-display-images-in-subfolders/#findComment-1173221 Share on other sites More sharing options...
dimi Posted February 12, 2011 Author Share Posted February 12, 2011 if the loop does not start at 0 you'll skip the first item, i ran the script without the function implementation. I 'm convinced the problem must be the parameters: $files = glob('$map/*.jpg'); I'm sure there 's something wrong with the ' or /* Quote Link to comment https://forums.phpfreaks.com/topic/227457-display-images-in-subfolders/#findComment-1173223 Share on other sites More sharing options...
denno020 Posted February 12, 2011 Share Posted February 12, 2011 I just removed the variable $map from being sent to glob(), and instead put the filepath there and it is now picking up the files, and displaying the code almost as desired. It still requires some tweaking, but the problem seemed to be in the variable being passed to the GetImages() function... Denno Quote Link to comment https://forums.phpfreaks.com/topic/227457-display-images-in-subfolders/#findComment-1173233 Share on other sites More sharing options...
denno020 Posted February 12, 2011 Share Posted February 12, 2011 I think i've just worked out what's happening. The variable being passed to the GetImages() functions isn't actually a directory, it's passing a filename. So using it in the glob function like this: glod('$map/*.jpg'); Won't actually work.. You need to save the directory into it's own variable, and then pass that variable to both the opendir() function and the glob() function. That doesn't make it work perfectly, however it's close.. Denno Quote Link to comment https://forums.phpfreaks.com/topic/227457-display-images-in-subfolders/#findComment-1173236 Share on other sites More sharing options...
denno020 Posted February 12, 2011 Share Posted February 12, 2011 Here ya go mate: function GetImages($map) { $files = glob("$map/*.jpg"); for ($i=0; $i<count($files); $i++) { $num = $files[$i]; echo '<img src="' . $num . '"/><br />'; } } $directory = './images'; if ($handle = opendir($directory)) { /* loop through directory. */ while (false !== ($dir = readdir($handle))) { if($dir != ".." && $dir != "."){ echo '<option value='.$dir.'>'.$dir.'</option><br>' . "\n"; GetImages($directory); } } closedir($handle); } Works a charm , oh except for the doubling and tripling up of images lol Denno Quote Link to comment https://forums.phpfreaks.com/topic/227457-display-images-in-subfolders/#findComment-1173240 Share on other sites More sharing options...
denno020 Posted February 12, 2011 Share Posted February 12, 2011 Did some further checking into the multiple displaying of images, and you can achieve the sme result with just 4 lines of code.. Try this: $directory = './images'; foreach(glob("$directory/*.jpg") as $filename){ //echo '<option value='.$dir.'>'.$dir.'</option><br>' . "\n"; echo '<img src="' . $filename . '"/><br />' . "\n"; } Exact same result. I have commented out the option tags as I'm not sure what you were aiming for there so I've left that up to you to work out how they should be written . Hope that helps Denno Quote Link to comment https://forums.phpfreaks.com/topic/227457-display-images-in-subfolders/#findComment-1173247 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.