Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
<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?

like image 685
Ben Miller Avatar asked Mar 02 '26 10:03

Ben Miller


1 Answers

You could use DirectoryIterator for this

foreach (new DirectoryIterator('/path/to/directory') as $fileInfo) {
    if($fileInfo->isDir() && !$fileInfo->isDot()) {
        // Do whatever
    }
}
like image 132
adlawson Avatar answered Mar 04 '26 22:03

adlawson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!