opendir and readdir acting funky

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

opendir and readdir acting funky

Post by hurdy gurdy »

Hola all, supernoob is back.

I've been banging my head against a wall trying to write a script that outputs the contents of a directory on a page. I've written it every way I know how (admittedly, that aint much). For some reason, PHP doesn't see the directory I'm trying to open (or just plainly denies its existance). The directory is there, filled to the brim with text files, whose names I want output to a page.

in case it was my meager writing skills at fault, I though I would use the code used in the manual, and I get te same results... nothing.

Is there a problem with the script?

Code: Select all

$dir = '/content/';
if (is_dir($dir)) {
	if ($dh = opendir($dir)) {
		while (($file = readdir($dh)) !== false) {
			echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
		 }
	closedir($dh);
	}
}
Any help on this issue would be greatly appreciated.

I just don't get it... it should work.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$dir = '/content/';
if (is_dir($dir)) {

$dir would need to be an absolute path (i.e /the/full/path/to/content) or relative to where the scripts running (i.e content). '/content/' won't use the document root if that's what you were trying to get ;)
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

Post by hurdy gurdy »

yep, that did it.

Funny, the manual has those slashes in their example...
Post Reply