Я сделал тест с этим деревом ниже.Здесь .tpe05
является скрытым каталогом, а tpe07
пустым.
.
├── index.php
├── tpe00
│ └── index.html
├── tpe01
│ └── index.html
├── tpe02
│ └── index.html
├── tpe03
│ └── index.html
├── tpe04
│ └── index.html
├── .tpe05
│ └── index.html
├── tpe06
│ └── index.html
├── tpe07
├── tpe08
│ └── index.html
└── tpe09
└── index.html
Давайте рассмотрим скрипт:
<?php
$title = array();
$link_html = array();
// Code added to make this test page working
// You can remove it since dirArray and indexCount
// seem to be already set in your code.
date_default_timezone_set('Europe/Paris');
$dirArray = array();
for ($i=0; $i <= 9 ; $i++)
array_push($dirArray,($i==5?".":null)."tpe0" . $i);
$indexCount = sizeof($dirArray);
// End of added code
// Getting *all* the files including the hidden ones.
// Getting also the hidden files.
// Filling title and link_html arrays for each file
// With <key,value> pairs.
// Examples:
// $title["tpe01"] = "Je suis index 01"
// $link_html["tpe01"] = "tpe01/index.html"
$fileList = glob('{,.}tpe*/index.html', GLOB_BRACE);
foreach($fileList as $file_Path) {
$html = file_get_contents($file_Path);
preg_match("/<title>([^<]*)<\/title>/im", $html, $matches);
$dirname = basename(dirname($file_Path));
$title[$dirname] = $matches[1];
$link_html[$dirname] = $file_Path;
}
// Code added to make this test page working (HTML table beginning)
// You can remove it since it seems you already begun the table.
echo "<table>";
// End of added code
// Loops through the array of files
for($index=0; $index < $indexCount; $index++) {
// Allows ./?hidden to show hidden files
$name=$dirArray[$index];
$hasDirPrefix = (substr($name, 0, 1) == ".");
if ($hasDirPrefix && !$showHiddenFile) continue;
if (!isset($title[$name])) continue;
// Gets File Names
// Gets Date Modified Data
$modtime=date("Y-m-d H:i", filemtime($dirArray[$index]));
$path = $link_html[$name]; // . ($showHiddenFile?"?hidden":null);
print("
<tr>
<td><a href='./$path'>$name</a></td>
<td><a href='./$path'>$modtime</a></td>
<td><a href='./$path'>$title[$name]</a></td>
</tr>"
);
}
// Code added to make this test page working (HTML table end)
// You can remove it since it seems you may have end the table
// further.
echo "</table>";
// End of added code
?>
Здесь я использовал continue
.Согласно документации PHP :
continue используется в структурах цикла для пропуска остальной части итерации текущего цикла и продолжениявыполнение при оценке состояния и затем начало следующей итерации .
Я также использовал ассоциативные массивы для заголовков и ссылок,Это стало намного легче получить их после.Посмотрите содержимое массива заголовков:
Array
(
[tpe00] => Je suis index 0
[tpe01] => Je suis index 1
[tpe02] => Je suis index 2
[tpe03] => Je suis index 3
[tpe04] => Je suis index 4
[tpe06] => Je suis index 6
[tpe08] => Je suis index 8
[tpe09] => Je suis index 9
[.tpe05] => Je suis index 5
)
Чтобы получить заголовок каталога tpe04
, просто напишите title["tpe04"]
.Вам не нужно обращать внимание на элементы заказа, что хорошо для нас: в $fileList
, ".tpe05"
- последний элемент, сделайте print_r($fileList);
, чтобы увидеть его.