如何遍历子目录?

时间:2011-09-20 19:48:02

标签: php dynamic foreach gallery dir

嘿伙计们快速的背景。主要在000webhost.com上使用php。非常新手/初学者。我从youtube和developphp.com上学到了大部分知识。

好的,我的问题。我试图在不使用flash的情况下构建动态图库页面。它必须是动态的,因为我希望用户能够上传图片然后将其显示在图库页面上。到现在为止还挺好。我正在寻找某种foreach(is_dir(/ Albums /))调用来遍历我的相册目录并查找所有文件夹然后将它们添加到页面中。现在我必须手动输入一切。这就是它的样子。

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

我想在身体里找到像......一样的东西。

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

无论如何要做到这一点还是我在我头上?欢迎任何帮助。

3 个答案:

答案 0 :(得分:8)

您可以将DirectoryIterator用于此

foreach (new DirectoryIterator('/path/to/directory') as $fileInfo) {
    if($fileInfo->isDir() && !$fileInfo->isDot()) {
        // Do whatever
    }
}

答案 1 :(得分:5)

你走在正确的轨道上!

这可以满足您的需要: 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
}

答案 2 :(得分:4)

虽然我很乐意提倡迭代器,但另一种选择是获取一系列文件夹。

glob('path/to/albums/*', GLOB_DIR)

如果你确实想要使用迭代器路由,那么ParentIterator会派上用场,轻松迭代目录。

$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
}