Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中读取以特定路径开始的目录中的所有文件_Php_File_Glob_File Read - Fatal编程技术网

如何在PHP中读取以特定路径开始的目录中的所有文件

如何在PHP中读取以特定路径开始的目录中的所有文件,php,file,glob,file-read,Php,File,Glob,File Read,我有类似于fm.txt fm_1.txt、fm_2.txt的文件名。当运行脚本时,我需要获得以fm开头的所有文件的计数 $files = glob(PATH_DIR.'_*.txt'); echo count($files); 显示0的计数 $files = glob(PATH_DIR.'*.txt'); echo count($files); 显示了1的计数 实际上我有3个文件,假设fm.txt,fm_1.txt,fm_2.txt。我猜第二个代码片段只显示了fm.txtcount,如何修改

我有类似于
fm.txt fm_1.txt、fm_2.txt
的文件名。当运行脚本时,我需要获得以fm开头的所有文件的计数

$files = glob(PATH_DIR.'_*.txt');
echo count($files);
显示0的计数

$files = glob(PATH_DIR.'*.txt');
echo count($files);
显示了1的计数


实际上我有3个文件,假设
fm.txt,fm_1.txt,fm_2.txt
。我猜第二个代码片段只显示了
fm.txt
count,如何修改该行以获得
\u
也包含的文件的cont

您可以使用readdir函数


您可以使用readdir函数


假设当前目录中有以下结构:

test.php
test
├── fm_1.txt
├── fm_2.txt
└── fm.txt
test.php

<?php
$dir = __DIR__ . '/test';
$files = glob("${dir}/fm{,_[0-9]*}.txt", GLOB_BRACE);
var_dump($files);

由于大括号扩展而导致的glob expressoin
fm{,[0-9]*}.txt
扩展为以下模式:

  • fm.txt
  • fm[0-9]*.txt

由于大括号中列出了两个项目:一个空值和一个
[0-9]*

假设当前目录中有以下结构:

test.php
test
├── fm_1.txt
├── fm_2.txt
└── fm.txt
test.php

<?php
$dir = __DIR__ . '/test';
$files = glob("${dir}/fm{,_[0-9]*}.txt", GLOB_BRACE);
var_dump($files);

由于大括号扩展而导致的glob expressoin
fm{,[0-9]*}.txt
扩展为以下模式:

  • fm.txt
  • fm[0-9]*.txt

由于大括号中列出了两项:一个空值和一个
[0-9]*

您需要告诉glob函数您想要以fm开头的文件,因此请尝试以下操作:


glob(路径到您的目录。“fm*.txt”)

您需要告诉glob函数您想要以fm开头的文件,所以请尝试以下操作:


glob(指向您的目录的路径。“fm*.txt”)

我猜
。如果您输出
$文件
,您将看到真实的图片。
我猜
。如果您输出
$files
,您将看到真实的图片。奇怪。我已经在我的本地设置上测试过了,效果很好。下面是我正在做的:我有一个目录/var/www/wms/public/test/,其中有4个文件:fm.txt、fm_1.txt、fm_2.txt、fm_3.txt$path=“/var/www/wms/public/test/”$files=glob($path.fm*.txt”);这给了我正确的结果。返回名称以fmStrange开头的所有文件。我已经在我的本地设置上测试过了,效果很好。下面是我正在做的:我有一个目录/var/www/wms/public/test/,其中有4个文件:fm.txt、fm_1.txt、fm_2.txt、fm_3.txt$path=“/var/www/wms/public/test/”$files=glob($path.fm*.txt”);这给了我正确的结果。返回名称以fm开头的所有文件