Jump to content

Archived

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

tjhilder

listing directories only?

Recommended Posts

Hi,

I have some code that lists everything in the directory it's pointing to, but I want to only list directories, I've had a good look around php.net but can't seem to find what i'm looking for so I was hoping someone here could give me a hand.

here's my code:
[code]
echo "<select name=\"album_name\">\n";
echo "\t<option selected=\"selected\" value=\"\">Select an Album</option>\n";
if ($handle = opendir("../../gallery/albums"))
{
while ($file = readdir($handle))
{
if (strlen($file) >3)
{
$files[] = $file;
}
}
closedir($handle);

sort($files);
foreach ($files as $file)
{
echo "\t<option value=\"{$file}\">$file</option>\n";
}
}
echo "</select>\n";
[/code]

--
TJ

Share this post


Link to post
Share on other sites
Take a look at the code from renich at woralelandia dot com on the manual's http://ca.php.net/manual/en/function.is-dir.php page. I *think* that's your solution.

Share this post


Link to post
Share on other sites
[code]if ($handle = opendir("../../gallery/albums"))
{
while ($file = readdir($handle))
{
if (strlen($file) >3 && is_dir($file))
{
$files[] = $file;
}
}
closedir($handle);

sort($files);
foreach ($files as $file)
{
echo "\t<option value=\"{$file}\">$file</option>\n";
}
}
echo "</select>\n";[/code]
Should do the trick.
I just added a check for [b]is_dir[/b] which checks if $file is a directory.

Share this post


Link to post
Share on other sites
Hi,

[b]AndyB[/b], thanks for the link but I've checked out that one a few times and i've tried different things but can't get it to work.

[b]ShogunWarrior[/b], that code doesn't work  :( using that code I get these errors:

[code]<select name="album_name">
<option selected="selected" value="">Select an Album</option>
<br />
<b>Warning</b>:  sort() expects parameter 1 to be array, null given in <b>/home/tjhilder/public_html/four/admin/one.php</b> on line <b>15</b><br />
<br />
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/home/tjhilder/public_html/four/admin/one.php</b> on line <b>16</b><br />

</select>
[/code]

Share this post


Link to post
Share on other sites
[b]readdir[/b] only returns the filename, so when you check [b]is_dir[/b] it's looking for the file in the current directory, this should work:
[code]
$dirname= "../../gallery/albums/";
if ($handle = opendir($dirname))
{
while ($file = readdir($handle))
{
if (strlen($file) >3 && is_dir($dirname.$file))
{
$files[] = $file;
}
}
closedir($handle);

sort($files);
foreach ($files as $file)
{
echo "\t<option value=\"{$file}\">$file</option>\n";
}
}
echo "</select>\n";
[/code]

Share this post


Link to post
Share on other sites

×

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.