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

Recommended Answers

All 4 Replies

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);

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

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.

Thanks blocblue that has sorted it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.