Jump to content


Photo

listing directories only?


  • Please log in to reply
5 replies to this topic

#1 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 15 July 2006 - 05:53 PM

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:
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";

--
TJ

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 15 July 2006 - 06:26 PM

Take a look at the code from renich at woralelandia dot com on the manual's http://ca.php.net/ma...tion.is-dir.php page. I *think* that's your solution.
Legend has it that reading the manual never killed anyone.
My site

#3 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 15 July 2006 - 06:27 PM

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";
Should do the trick.
I just added a check for is_dir which checks if $file is a directory.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#4 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 15 July 2006 - 09:05 PM

Hi,

AndyB, 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.

ShogunWarrior, that code doesn't work  :( using that code I get these errors:

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


#5 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 15 July 2006 - 10:28 PM

readdir only returns the filename, so when you check is_dir it's looking for the file in the current directory, this should work:
$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";

<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#6 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 16 July 2006 - 09:49 PM

thanks so much, it works great!  ;D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users