0

Hi Guys

I am currently using the opendir to display all files and folders in a specific directory code below. It displays great however it display each file twice. If it is a folder then it will just display once. I have check the folder on the server and there is only the one version within the folder. I have used the same code on another part of the site and it works perfectly.

<select name="deleteFile">
    <option value="">select file</option>
    <?   $folder = "../course/".$dir."";
        $handle = opendir($folder); 

        # Making an array containing the files in the current directory: 
        while ($file = readdir($handle)) { 
            $files[] = $file; 
        } 
        closedir($handle); 

        #echo the files 
        foreach ($files as $file) { 
            if($file!='.' && $file!='..') {
                echo('<option value="'.$file.'">'.$file.'</option>'); 
            }
        }  ?>
</select>

Any help will be great.

Cheers
Neil

3
Contributors
4
Replies
5
Views
4 Years
Discussion Span
Last Post by shuka79
0

Works fine for me, maybe it's the $dir variable? As suggestion use print_r() on line 11, just before the while loop, to check the $files array:

print_r($files);
0

Thanks for your reply
I have put in the print_r, the the output included duplicates still

I have also hardcode in the directory instead of using $dir variable but not made any difference.
Cheers
Neil

1

I have used the same code on another part of the site and it works perfectly.

I don't see that you've reinitialised the $files array anywhere. It could be populated with the results of the last time you used the code.

Add $files = array(); immediately after line 4.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.