Pregunta

I am currently using scandir to get the contents of an upload folder. Here is my code specifying the directory:

$dir    = /var/www/vhosts/mywebsite.com/httpdocs/admin/newsletters/1234-16-10-2013/
$files = scandir($dir);

I then loop through the results like this:

foreach($files as $file) {

  echo '<option value="'.$file.'">'.$file.'</option>';    

}

The above works fine and populates my select menu correctly. However for some reason the options in the select menu look like this:

<option value=".">.</option>
<option value="..">..</option>
<option selected="selected" value="header.jpg">header.jpg</option>
<option value="sale.jpg">sale.jpg</option>
<option value="show-now.jpg">show-now.jpg</option>

The first 2 options contains full stops. The first has 1 and the second has 2.

Does anyone know why this is? Is it because of the depth of the directory?

Any help would be greatly appreciated!

¿Fue útil?

Solución

That's because of system entries . and .. which are pointers to 'current directory' and 'parent directory'. You'll need to filter them if you don't want to see them in your output.

You may want to filter your entries with is_file() - then . and .. would be skipped since they are actually directories (pointers to them)

Otros consejos

Scandir is relatively slow.

        foreach (new DirectoryIterator($path) as $file)
        {
            if($file->isDot()) continue;

            if($file->isDir()))
            {
                print $file->getFilename() . '<br />';
            }
        }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top