Jump to content

display images in subfolders


dimi

Recommended Posts

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);
} 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 /*

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.