Jump to content

listing directories only?


tjhilder

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
Link to comment
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.
Link to comment
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]
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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