...

View Full Version : . and .. from directory



Dat
11-18-2007, 10:31 PM
Here is a code that will read all the files in the directory and output them as links to them but on the page it add's in 2 extra links before outputting the list:

<a href="/schedule/.">.</a><br><a href="/schedule/..">..</a><br>

Here is the PHP:

$dir = "./schedule/";

if(is_dir($dir) && is_readable($dir))
{
$dh = opendir($dir);
while(($file = readdir($dh)) !== false)
{
echo "<a href=\"/schedule/".$file."\">".$file."</a><br>";
}
}
else
{
echo 'Invalid directory';
}

rpgfan3233
11-18-2007, 11:00 PM
So you want to get rid of them? You can do something like:

while((($file = readdir($dh)) !== false) && (strcmp($file, '.') != 0) && (strcmp($file, '..') != 0))

Dat
11-18-2007, 11:07 PM
It didn't rid of it and I was wondering why . and .. was included in the list?

Majoracle
11-18-2007, 11:27 PM
Not sure why they show, but this will get rid of them:


$dir = "./schedule/";

if(is_dir($dir) && is_readable($dir))
{
$dh = opendir($dir);
while(($file = readdir($dh)) !== false)
{
if($file != '.' && $file != '..'){
echo "<a href=\"/schedule/".$file."\">".$file."</a><br>";
}
}
closedir($dh);
}
else
{
echo 'Invalid directory';
}

Also, should close the directory when done (also added that).

felgall
11-19-2007, 01:23 AM
They show up because every directory has a link to itself . and its parent .. in addition to links to all the files it contains. They are needed for the operating system to work when you want to use relative addresses.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum