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
https://forums.phpfreaks.com/topic/272375-recursive-image-search/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.