php 从 scandir 中排除隐藏文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8532569/
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
Exclude hidden files from scandir
提问by Sino
I am using the following code to get a list of images in a directory:
我正在使用以下代码获取目录中的图像列表:
$files = scandir($imagepath);
but $files
also includes hidden files. How can I exclude them?
但$files
也包括隐藏文件。我怎样才能排除它们?
回答by mario
回答by robjmills
I tend to use DirectoryIterator for things like this which provides a simple method for ignoring dot files:
我倾向于将 DirectoryIterator 用于这样的事情,它提供了一种忽略点文件的简单方法:
$path = '/your/path';
foreach (new DirectoryIterator($path) as $fileInfo) {
if($fileInfo->isDot()) continue;
$file = $path.$fileInfo->getFilename();
}
回答by Henrik Albrechtsson
function nothidden($path) {
$files = scandir($path);
foreach($files as $file) {
if ($file[0] != '.') $nothidden[] = $file;
return $nothidden;
}
}
Simply use this function
只需使用此功能
$files = nothidden($imagepath);
回答by Agnel Vishal
$files = array_diff(scandir($imagepath), array('..', '.'));
or
或者
$files = array_slice(scandir($imagepath), 2);
might be faster than
可能比
$files = preg_grep('/^([^.])/', scandir($imagepath));
回答by Oytun
I encountered a comment from php.net, specifically for Windows systems: http://php.net/manual/en/function.filetype.php#87161
我遇到了来自 php.net 的评论,专门针对 Windows 系统:http: //php.net/manual/en/function.filetype.php#87161
Quoting here for archive purposes:
引用此处用于存档目的:
I use the CLI version of PHP on Windows Vista. Here's how to determine if a file is marked "hidden" by NTFS:
function is_hidden_file($fn) { $attr = trim(exec('FOR %A IN ("'.$fn.'") DO @ECHO %~aA')); if($attr[3] === 'h') return true; return false; }
Changing
if($attr[3] === 'h')
toif($attr[4] === 's')
will check for system files.This should work on any Windows OS that provides DOS shell commands.
我在 Windows Vista 上使用 CLI 版本的 PHP。以下是确定文件是否被 NTFS 标记为“隐藏”的方法:
function is_hidden_file($fn) { $attr = trim(exec('FOR %A IN ("'.$fn.'") DO @ECHO %~aA')); if($attr[3] === 'h') return true; return false; }
更改
if($attr[3] === 'h')
为if($attr[4] === 's')
将检查系统文件。这应该适用于任何提供 DOS shell 命令的 Windows 操作系统。
回答by TiMacDonald
I reckon because you are trying to 'filter' out the hidden files, it makes more sense and looks best to do this...
我认为因为您正在尝试“过滤”隐藏文件,所以这样做更有意义并且看起来最好......
$items = array_filter(scandir($directory), function ($item) {
return 0 !== strpos($item, '.');
});
I'd also not call the variable $files
as it implies that it only contains files, but you could in fact get directories as well...in some instances :)
我也不会调用该变量,$files
因为它暗示它只包含文件,但实际上您也可以获取目录......在某些情况下:)
回答by Vrushal Raut
use preg_grep to exclude files name with special characters for e.g.
使用 preg_grep 排除带有特殊字符的文件名,例如
$dir = "images/";
$files = preg_grep('/^([^.])/', scandir($dir));
回答by Black
Use the following code if you like to reset the array index too and set the order:
如果您也想重置数组索引并设置顺序,请使用以下代码:
$path = "the/path";
$files = array_values(
preg_grep(
'/^([^.])/',
scandir($path, SCANDIR_SORT_ASCENDING)
));
One line:
一条线:
$path = "daten/kundenimporte/";
$files = array_values(preg_grep('/^([^.])/', scandir($path, SCANDIR_SORT_ASCENDING)));
回答by stefandoorn
Assuming the hidden files start with a .
you can do something like this when outputting:
假设隐藏文件以 a 开头,.
您可以在输出时执行以下操作:
foreach($files as $file) {
if(strpos($file, '.') !== (int) 0) {
echo $file;
}
}
Now you check for every item if there is no .
as the first character, and if not it echos you like you would do.
现在您检查每个项目,如果没有.
作为第一个字符,如果没有,它会像您一样回应您。
回答by calmcajun
I am still leaving the checkmark for seengee's solution and I would have posted a comment below for a slight correction to his solution.
我仍在为seengee的解决方案留下复选标记,我会在下面发表评论以对他的解决方案进行轻微的修正。
His solution masks the directories(. and ..) but does not mask hidden files like .htaccess
他的解决方案屏蔽了目录(. 和 ..),但没有屏蔽像 .htaccess 这样的隐藏文件
A minor tweak solves the problem:
一个小的调整解决了这个问题:
foreach(new DirectoryIterator($curDir) as $fileInfo) {
//Check for something like .htaccess in addition to . and ..
$fileName = $fileInfo->getFileName();
if(strlen(strstr($fileName, '.', true)) < 1) continue;
echo "<h3>" . $fileName . "</h3>";
}