PHP:如何在不列出子目录的情况下列出目录中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7684881/
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
PHP: How to list files in a directory without listing subdirectories
提问by Pinkie
This is the starting portion of my code to list files in a directory:
这是我的代码的起始部分,用于列出目录中的文件:
$files = scandir($dir);
$array = array();
foreach($files as $file)
{
if($file != '.' && $file != '..' && !is_dir($file)){
....
I'm trying to list all files in a directory without listing subfolders. The code is working, but showing both files and folders. I added !is_dir($file)
as you see in my code above, but the results are still the same.
我试图列出目录中的所有文件而不列出子文件夹。代码正在运行,但同时显示文件和文件夹。我!is_dir($file)
在上面的代码中添加了您看到的内容,但结果仍然相同。
回答by rukya
It should be like this, I think:
应该是这样的,我想:
$files = scandir($dir);
foreach($files as $file)
{
if(is_file($dir.$file)){
....
回答by Aurelio De Rosa
Just use is_file
.
只需使用is_file
.
Example:
例子:
foreach($files as $file)
{
if( is_file($file) )
{
// Something
}
}
回答by user1403517
What a pain for something so seemingly simple! Nothing worked for me... To get a result I assumed the file name had an extension which it must in my case.
看起来如此简单的事情真是太痛苦了!没有什么对我有用......为了得到结果,我假设文件名有一个扩展名,在我的情况下它必须。
if ($handle = opendir($opendir)) {
while (false !== ($entry = readdir($handle))) {
$pos = strpos( $entry, '.' );
if ($entry != "." && $entry != ".." && is_numeric($pos) ) {
............ good entry
…… 好入门
回答by Eduardo Güereque
This will scan the files then check if . or .. is in an array. Then push the files excluding . and .. in the new files[] array.
这将扫描文件,然后检查 . 或 .. 在一个数组中。然后推送不包括 . 和 .. 在新的 files[] 数组中。
Try this:
尝试这个:
$scannedFiles = scandir($fullPath);
$files = [];
foreach ($scannedFiles as $file) {
if (!in_array(trim($file), ['.', '..'])) {
$files[] = $file;
}
}
回答by rplaurindo
Use the DIRECTORY_SEPARATOR
constant to append the file to its directory path too.
也可以使用DIRECTORY_SEPARATOR
常量将文件附加到其目录路径。
function getFileNames($directoryPath) {
$fileNames = [];
$contents = scandir($directoryPath);
foreach($contents as $content) {
if(is_file($directoryPath . DIRECTORY_SEPARATOR . $content)) {
array_push($fileNames, $content);
}
}
return $fileNames;
}
回答by JLDN Admin
This is a quick and simple one liner to list ONLY files. Since the user wants to list only files, there is no need to scan the directory and return all the contents and exclude the directories. Just get the files of any type or specific type. Use * to return all files regardless of extension or get files with a specific extension by replacing the * with the extension.
这是一个快速简单的单行列表,仅列出文件。由于用户只想列出文件,因此无需扫描目录并返回所有内容并排除目录。只需获取任何类型或特定类型的文件。使用 * 返回所有文件而不考虑扩展名,或者通过将 * 替换为扩展名来获取具有特定扩展名的文件。
Get all files regardless of extension:
获取所有文件而不考虑扩展名:
$files = glob($dir . DIRECTORY_SEPARATOR . "*");
Get all files with the php extension:
获取所有带有 php 扩展名的文件:
$files = glob($dir . DIRECTORY_SEPARATOR . "*.php");
Get all files with the js extension:
获取所有扩展名为 js 的文件:
$files = glob($dir . DIRECTORY_SEPARATOR . "*.js");
I use the following for my sites:
我对我的网站使用以下内容:
function fileList(string $directory, string $extension="") :array
{
$filetype = '*';
if(!empty($extension) && mb_substr($extension, 0, 1, "UTF-8") != '.'):
$filetype .= '.' . $extension;
else:
$filetype .= $extension;
endif;
return glob($directory . DIRECTORY_SEPARATOR . $filetype);
}
Usage :
用法 :
$files = fileList($configData->includesDirectory, '');
With my custom function, I can include an extension or leave it empty. Additionally, I can forget to place the . before the extension and it will succeed.
使用我的自定义函数,我可以包含扩展名或将其留空。此外,我可能会忘记放置 . 在扩展之前,它会成功。