在scandir数组中包含JUST文件? [英] Include JUST files in scandir array?

查看:93
本文介绍了在scandir数组中包含JUST文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要从scandir返回的数组,但是它包含".""..",我不希望这样.

I have an array I'm getting back from scandir, but it contains "." and ".." and I don't want it to.

我的代码:

$indir = scandir('../pages');
$fileextensions = array(".", "php", "html", "htm", "shtml");
$replaceextensions = str_replace($fileextensions, "", $indir);

我正在对文件扩展名进行字符串替换,因此导致[0]和[1]显示为空,但它们分别为"."".."

I am doing a string replace on the file extensions, thus causing [0] and [1] to appear empty, but they are "." and ".."

array(4) {
[0]=>
string(0) ""
[1]=>
string(0) ""
[2]=>
string(4) "test"
[3]=>
string(4) "home"
}

如何从数组中删除"."".."?

推荐答案

您可以使用 array_filter .

$indir = array_filter(scandir('../pages'), function($item) {
    return !is_dir('../pages/' . $item);
});

请注意,这会过滤掉所有目录,仅保留文件和符号链接.如果您确实只想排除以.开头的文件(和目录),则可以执行以下操作:

Note this filters out all directories and leaves only files and symlinks. If you really want to only exclude only files (and directories) starting with ., then you could do something like:

$indir = array_filter(scandir('../pages'), function($item) {
    return $item[0] !== '.';
});

这篇关于在scandir数组中包含JUST文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆