PDA

View Full Version : . and .. from directory



Dat
11-18-2007, 11: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-19-2007, 12:00 AM
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-19-2007, 12:07 AM
It didn't rid of it and I was wondering why . and .. was included in the list?

Majoracle
11-19-2007, 12:27 AM
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, 02: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.