php 在 scandir 数组中包含 JUST 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14680121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Include JUST files in scandir array?
提问by Rbn
I have an array I'm getting back from scandir, but it contains "."and ".."and I don't want it to.
我有一个数组,我正从SCANDIR回来,但它包含了"."和".."我不希望它。
My code:
我的代码:
$indir = scandir('../pages');
$fileextensions = array(".", "php", "html", "htm", "shtml");
$replaceextensions = str_replace($fileextensions, "", $indir);
I am doing a string replace on the file extensions, thus causing [0] and [1] to appear empty, but they are "."and ".."
我正在对文件扩展名进行字符串替换,从而导致 [0] 和 [1] 显示为空,但它们是"."和".."
array(4) {
[0]=>
string(0) ""
[1]=>
string(0) ""
[2]=>
string(4) "test"
[3]=>
string(4) "home"
}
How would I remove the "."and ".."from the array?
我将如何从数组中删除"."和".."?
回答by leftclickben
You can use array_filter.
您可以使用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] !== '.';
});
回答by mintedsky
Fastest way to remove dots as files in scandir
在scandir中删除点作为文件的最快方法
$files = array_slice(scandir('/path/to/directory/'), 2);
From the PHP Manual
来自PHP 手册
回答by Jonathan Wren
array_diffwill do what you're looking for:
array_diff会做你正在寻找的:
$indir = scandir('../pages');
$fileextensions = array(".", "php", "html", "htm", "shtml");
$indir = array_diff($indir, array('.', '..'));
$replaceextensions = str_replace($fileextensions, "", $indir);
回答by FluorescentGreen5
I am aware erknrio provided an answer for this, but here is a cleaner way of getting an array of files without directories (modified to be more efficient):
我知道 erknrio 为此提供了一个答案,但这里有一种更简洁的方法来获取没有目录的文件数组(修改为更有效):
$dirPath = 'dashboard';
$dir = scandir($dirPath);
foreach($dir as $index => &$item)
{
if(is_dir($dirPath. '/' . $item))
{
unset($dir[$index]);
}
}
$dir = array_values($dir);
回答by Devdutt Sharma
simply use preg_replace to remove all kind of hidden's file from directory
只需使用 preg_replace 从目录中删除所有类型的隐藏文件
$files = array(".", "..", "html", ".~html", "shtml");
$newfiles = preg_grep('/^([^.])/', scandir($files));
回答by mrroot5
You can use this snippet. It returns just files in directory:
您可以使用此代码段。它只返回目录中的文件:
function only_files($dir_element) {
if (!is_dir($dir_element)) {
return $dir_element;
}
}
function givemefiles($dir) {
$scanned_dir = scandir($dir);
return array_filter($scanned_dir, "only_files");
}
$dir_path = '../pages';
givemefiles($dir_path);
echo "<pre>";
var_dump($scanned_dir);
echo "</pre>";

