Probelm displaying files from directory

Hello folks,

Please view the following page: http://www.roy.lu/futuraplay/

I am using this code:


$dir="./download"; // Directory where files are stored

if ($dir_list = opendir($dir))
{
while(($filename = readdir($dir_list)) !== false)
{
?>
<p><a href="<?php echo $filename; ?>"><?php echo $filename;
?></a></p>
<?php
}
closedir($dir_list);
}

1. Why does this display a . and a in addition to the files in the directory? - I don’t want this
2. Why don’t the links include the real file path? Hence the pictures won’t open…

Thanks in advance for your help.

Roy

Because you didn’t ask it to differentiate between files and directories. Each folder in linux contains a . and … for current folder and parent folder. You can use is_file($dir . ‘/’ . $filename) to get rid of them.

Because $filename is just the file name. You need to use $dir . ‘/’ . $filename to get the full path.

Ok, I understand what you are saying, but I can’t figure out where/ how i need to change my code.
A little help please?

Certainly,

<?php
$dir="./download"; // Directory where files are stored 

if ($dir_list = opendir($dir)) 
{ 
   while(($filename = readdir($dir_list)) !== false) 
   {
      $pathToFile = $dir . '/' . $filename;
      if (is_file($pathToFile)) // added this and changed the link to use $pathToFile
      { 
?> 
   <p><a href="<?php echo $pathToFile; ?>"><?php echo $filename; ?></a></p> 
<?php 
      }
   } 
   closedir($dir_list); 
}
?>

Thanks a lot, I did manage to get the link working properly, but I’m not familiar enough with PHP yet to do this kind of magic…
I was trying something along the lines of:

if (($dir_list = opendir($dir)) && is_file($dir . ‘/’ . $filename))
{ …

but this wasn’t as logical to PHP as it seemed to me :wink: