在PHP中打开并读取文件夹内目录中的文件
好了,伙计们,我有点不知道如何调整我的代码。到目前为止,我的代码可以读取目录中的任何Json文件,对其进行解析并将其放入一个表中,这非常有用,因为我每行仅使用1个Json文件 我现在需要做的是,我拥有的每个IP地址都会给我3个JSON文件,它们被放入一个以IP地址为名称的文件夹中。在我的主目录中,我将有许多文件夹,每个文件夹中有3个JSON文件 我想读取每个文件夹中的每个文件,将我解析的信息放在一个表中,然后作为新行移动到下一个文件夹,并执行相同的操作在PHP中打开并读取文件夹内目录中的文件,php,json,parsing,Php,Json,Parsing,好了,伙计们,我有点不知道如何调整我的代码。到目前为止,我的代码可以读取目录中的任何Json文件,对其进行解析并将其放入一个表中,这非常有用,因为我每行仅使用1个Json文件 我现在需要做的是,我拥有的每个IP地址都会给我3个JSON文件,它们被放入一个以IP地址为名称的文件夹中。在我的主目录中,我将有许多文件夹,每个文件夹中有3个JSON文件 我想读取每个文件夹中的每个文件,将我解析的信息放在一个表中,然后作为新行移动到下一个文件夹,并执行相同的操作 FOR REFERENCE:: Curr
FOR REFERENCE::
Current file layout:
FOLDER-->JSON
-->JSON
-->JSON
New file layout:
FOLDER-->IPADDRESS-->JSONFILE1
-->JSONFILE2
-->JSONFILE3
-->IPADDRESS2-->JSONFILE1
--JSONFILE2
-->JSONFILE3
读取目录中任何JSON文件的当前代码:
$dir = "my dir";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
foreach(glob("*_name.json") as $filename) {
$data = file_get_contents($filename);
$testing = json_decode($data, true);
echo "<tr>";
echo "<td>{$filename }</td>";
foreach($testing[0] as $row) {
// code for parsing here ...
}
}
}
}
$dir=“我的目录”;
if(is_dir($dir)){
如果($dh=opendir($dir)){
foreach(glob(“*\u name.json”)作为$filename){
$data=file\u get\u contents($filename);
$testing=json_decode($data,true);
回声“;
回显“{$filename}”;
foreach($testing[0]作为$row){
//这里的解析代码。。。
}
}
}
}
在这里使用递归迭代器类
function Get_Files()
{
$dir = "my_dir";
$init = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
$files = array();
foreach ($init as $file) {
if ($file->isDir()) {
continue;
}
$files[] = $file->getPathname();
}
return $files;
}
foreach (Get_Files() as $file) {
$data = file_get_contents($file);
$testing = json_decode($data, true);
echo "<tr>";
echo "<td>{$file}</td></tr>";
}
my_dir\192.168.0.1\JSONFILE1.json
my_dir\192.168.0.1\JSONFILE2.json