[英]Include JUST files in scandir array?
I have an array I'm getting back from scandir, but it contains "."
我有一个从 scandir 返回的数组,但它包含"."
and ".."
and I don't want it to.和".."
我不想这样。
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 "."
我正在对文件扩展名进行字符串替换,从而导致 [0] 和 [1] 显示为空,但它们是"."
and ".."
和".."
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?和数组中的".."
?
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] !== '.';
});
Fastest way to remove dots as files in scandir 在scandir中删除点作为文件的最快方法
$files = array_slice(scandir('/path/to/directory/'), 2);
From the PHP Manual 从PHP手册
array_diff
will 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);
http://php.net/manual/en/function.array-diff.php http://php.net/manual/en/function.array-diff.php
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);
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>";
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));
If you need a clean function;如果你需要一个干净的功能;
function getFiles($dir){
return array_values(array_filter(scandir($dir), function($file){
global $dir;
return !is_dir("{$dir}/{$file}");
}));
}
I think this function is both neat and will work well.我认为这个功能既简洁又可以很好地工作。 Also, the keys of the returned array in this function are sorted correctly.此外,此函数中返回数组的键已正确排序。
All other answers are good.所有其他答案都很好。 I have a clean workaround that worked for me.我有一个对我有用的干净的解决方法。
Use glob function that takes a regex and returns only files that match.使用glob function 接受正则表达式并仅返回匹配的文件。 It seems to exclude the .
似乎排除了.
and ..
directories, so using a simple和..
目录,所以使用一个简单的
$indir = glob('../pages/*');
should work well.应该运作良好。
function dscan( $path ){
global $dfiles;
if( !isset( $dfiles ) ){
$dfiles = array();
}
$files = preg_grep('/^.*\.php$/i', scandir($path));
foreach( $files as $file ){
if( !in_array( "$path/$file",$dfiles ) ){
$dfiles[] = array( 'path' => "$path/$file", 'name' => $file );
}
}
return $dfiles;
}
dscan( $path );
print_r( $dfiles );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.