Jump to content

Problems outputting file names using readdir()


j.smith1981

Recommended Posts

Hi there I am having a problem outputting file names in PHP using readdir() function.

 

The problem is with this code below:

 

<?php
if(is_dir($directory)) {
  
  if($dh = opendir($directory)) {
    while(($file = readdir($dh)) !== false) {
  echo $file;
}
    closedir($dh);
  }
}
?>

 

With $directory obviously being the directory I wanted to output allowing me to let users say change the style sheets they where using, going through a book of php work you see in Hacks (meaning of course making PHP do things you didn't realise where possible or knew how to do, or a very hit and miss approach to getting work done as quickly as possible and the like), it's really expanding my capabilities in PHP.

 

Though everytime I try and output the file names in which ever directory there's this kind of output:

.myfilename.extension..

 

Where there's one leading fullstop/period (.) and then 2 trailing ones, is there any bette method that gets rid of these entirely please?

 

Would really appreciate your help on this and thank you for your help in advance too!

Jeremy.

 

the . and the .. are actually directory references as well, since you did not tell the script to add a newline for each directory..

 

<?php
if ($handle = opendir('.')) { //insert directory
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            echo "$file\n";
        }
    }
    closedir($handle);
}
?> 

Ahh ok thank you ever so much!

 

I mean the example in the book made sense to me however I was going off the PHP manual to really get to grips with the books example in Hacks in PHP

 

Thanks ever so much again though,

Jeremy.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.