[英]php - listing folders and files in a directory
hi there i am using the following function to list all the files and folders in a directory. 嗨,我正在使用以下功能列出目录中的所有文件和文件夹。
<?php
function listFolderFiles($dir){
$ffs = scandir($dir);
foreach($ffs as $ff){
echo $ff . "<br/>";
}
}
?>
but the problem seems to be i'm getting all the folders in the directory alright but i'm also getting a .
但是问题似乎是我正在获取目录中的所有文件夹,但是我也正在获取
.
and a ..
. 还有一个
..
something like the one below. 类似于下面的内容。
.
..
direc
img
music
New Text Document.txt
and i am using the following function like: listFolderFiles('MyFolder');
我正在使用以下功能,如:
listFolderFiles('MyFolder');
what i want to do is get all the folders and files but not the .
我想要做的是获取所有文件夹和文件,但不获取
.
and the ..
, what have i done wrong and how can i get what i want. 和
..
,我做错了什么,我怎么能得到我想要的。 thanks! 谢谢!
Easy way to get rid of the dots that scandir()
picks up in Linux environments: 摆脱
scandir()
在Linux环境中出现的问题的简单方法:
<?php
$ffs = array_diff(scandir($dir), array('..', '.'));
?>
You can use glob quite easily, which puts the filenames into an array: 您可以非常容易地使用glob,它将文件名放入数组中:
print_r(glob("*.*"));
example: 例:
// directory name
$directory = "/";
// get in directory
$files = glob($directory . "*");
$d = 0; // init dir array count
$f = 0; // init file array count
// directories and files
foreach($files as $file) {
if(is_dir($file)) {
array($l['directory'][$d] = $file);
$d++;
} else {
array($l['file'][$f] = $file);
$f++;
}
}
print_r($l);
NOTE : scandir
will also pick up hidden files such as .htaccess
, etc. That is why the glob method should be considered instead, unless of course you want to show them. 注意 :
scandir
还将拾取诸如.htaccess
隐藏文件。这就是为什么应该考虑使用glob方法的原因,除非您当然想要显示它们。
This should do it! 这应该做!
<?php
function listFolderFiles($dir){
$ffs = scandir($dir);
foreach($ffs as &$ff){
if ($ff != '.' && $ff != '..') {
echo $ff . "<br/>";
}
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.