Jump to content

Recursive Image Search


HCProfessionals

Recommended Posts

I am having a difficult time displaying arrays to an actual file list

 

What I currently have for code:

<?php
$dir = 'download/';
$results = array();
if (is_dir($dir)) {
$iterator = new RecursiveDirectoryIterator($dir);
foreach ( new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file ) {
if ($file->isFile()) {
$thispath = str_replace('\\', '/', $file);
$thisfile = utf8_encode($file->getFilename());
$results = array_merge_recursive($results, pathToArray($thispath));
}
}
}
echo "<pre>";
print_r($results);


function pathToArray($path , $separator = '/') {
if (($pos = strpos($path, $separator)) === false) {
return array($path);
}
return array(substr($path, 0, $pos) => pathToArray(substr($path, $pos + 1)));
}
?>

 

Which Will Display:


Array
(
[download] => Array
(
[folder1] => Array
(
[0] => image1.jpg
[1] => image2.jpg
[2] => image3.jpg
[3] => image4.jpg
[4] => image5.jpg
)

[folder2] => Array
(
[0] => image1.jpg
[1] => image2.jpg
[2] => image3.jpg
[3] => image4.jpg
[4] => image5.jpg
)
)
)

 

What I am looking for as a display:

 

- Not to show the download folder, only subs

 

<div class="images">
<div title="folder1">
<img src="download/folder1/image1.jpg" />
<img src="download/folder1/image2.jpg" />
<img src="download/folder1/image3.jpg" />
<img src="download/folder1/image4.jpg" />
<img src="download/folder1/image5.jpg" />
</div>

<div title="folder2">
<img src="download/folder2/image1.jpg" />
<img src="download/folder2/image2.jpg" />
<img src="download/folder2/image3.jpg" />
<img src="download/folder2/image4.jpg" />
<img src="download/folder2/image5.jpg" />
</div>
</div>

Link to comment
Share on other sites

Are you using recursive itteration because your directory structure is going to change (i.e. unknown number of tiers)? If so, you should generate the output during your recursion; Otherwise, you will have to recursively itterate through your generated array because it will have an unknown structure as well.

 

If you could be more specific about what you are trying to accomplish, I can give you some example code.

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.