doucaishi0077 2013-10-17 00:22
浏览 43
已采纳

php使用scandir获取文件夹内容

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!

  • 写回答

2条回答 默认 最新

  • doufei3152 2013-10-17 00:24
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?