How do I loop through subdirectories?
I asing php mainly in 000webhost.com. Very novice/beginner. I learned most of what I know from youtube and developphp.com .
Okay so my question. I am trying to build a dynamic gallery page without using flash. It has to be dynamic because i want a user to be able to upload a picture then have it show up on the gallery page. So far so good. I am looking for some sort of foreach (is_dir(/Albums/)) call to loop through my albums directory and look for all folders and then add them to the page. Right now i have to manually input everything. This is what it looks like.
<table width="100%"><tr><td width="78%"><span class="textbg">Miller Albums</span></td><td width="22%"><?php echo $toplinks; ?></td></tr></table><span class="textsm"></span><p class="desc"></p><p><span class="textreg">Click an album to see the pictures</span><br><a href="http://miller.netai.net/member_profile.php?id=<?php echo $id; ?>">Home</a>
</p><hr size="1">
<table style="text-align: center">
<tbody>
<tr>
<td>
<a href="/Albums/CRFA"><img width="150" height="150" border="0" align="middle" title="CRFA" src="/Albums/CRFA/thumbnail.jpg"></a>
</td>
<td>
<开发者_C百科a href="/Albums/Internet%20Pics"><img width="150" height="150" border="0" align="middle" title="Internet Pics" src="/Albums/Internet%20Pics/thumbnail.jpg"></a>
</td>
<td>
<a href="/Albums/Dads%20Wedding"><img width="150" height="150" border="0" align="middle" title="Dads Wedding" src="/Albums/Dads Wedding/thumbnail.jpg"></a>
</td>
<td>
<a href="/Albums/Dads%20Wedding%20(cont.)"><img width="150" height="150" border="0" align="middle" title="Dads Wedding (cont.)" src="/Albums/Dads Wedding (cont.)/thumbnail.jpg"></a>
</td>
</tr>
<tr>
<td>
<label><font size="3">Ben's Fire Academy</font></label>
</td>
<td>
<label><font size="3">Internet Pics</font></label>
</td>
<td>
<label><font size="3">Dad's Wedding</font></label>
</td>
<td>
<label><font size="3">Dad's Weding (cont.)</font></label>
</td>
</tr></tbody></table>
<map name="Map">
<area href="frameset.htm" coords="95,1,129,44" shape="rect">
</map>
</body>
I'd like to have something in the body like...
<table style="text-align: center">
<tbody>
<tr>
<?php
foreach(is_dir(/Albums/))
<td>
<a href="/Albums/%dir%">
<img width="150 height="150" border="0" align="middle" title="%dir%" src="/Albums/%dir%/thumbnail.jpg"></a>
</td>
IF no_more_dir
exit();
?>
</tr>
Is there anyway to do this or am I in way over my head?
You could use DirectoryIterator
for this
foreach (new DirectoryIterator('/path/to/directory') as $fileInfo) {
if($fileInfo->isDir() && !$fileInfo->isDot()) {
// Do whatever
}
}
You're on the right track!
This does what you need it to do: http://php.net/manual/en/class.recursivedirectoryiterator.php
$album_dir = new RecursiveDirectoryIterator('path/to/album_root');
foreach (new RecursiveIteratorIterator($album_dir) as $filename => $file) {
// echo img tag
}
While I'd love to advocate the iterators, another option would be to get an array of folders with.
glob('path/to/albums/*', GLOB_DIR)
If you do want to go the iterator route, then a ParentIterator
would come in handy to easily only iterate over directories.
$dirs = new ParentIterator(new RecursiveDirectoryIterator('path/to/albums'););
$it = new RecursiveIteratorIterator($dirs, RecursiveIteratorIterator::SELF_FIRST);
// Optionally, limit the depth of recursion with $it->setMaxDepth($number)
foreach ($it as $dir) {
// Output your HTML here
}
精彩评论