Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.