doucaishi0077 2013-10-17 08:22
浏览 42
已采纳

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 08: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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改