在网站HTML表格中,我需要在表格中填充位于网站特定目录中的PDF文件的名称。
示例:
表左侧年份:2010年,2011年,2012年 表格上方的月份:Jan,Feb,Mar
数据记录需要从网站的根目录中提取结构化文件夹:
html_public / UPLOADEDFILES / files_type_a / 2010 / 01jan html_public / UPLOADEDFILES / files_type_a / 2010 / 02feb html_public / UPLOADEDFILES / files_type_a / 2010 / 03mar
因此,已上传到/ 01jan文件夹的PDF文档会在HTML表格的相应单元格中显示该PDF文件的名称。
答案 0 :(得分:3)
此PHP代码将遍历您指定的目录,并将找到的所有PDF文件放入名为$files
的数组中。您可能需要调整$dir
。
$dir = 'html_public/uploadedfiles/files_type_a/2010/'; //directory to pull from
$skip = array('.','..'); //a few directories to ignore
$dp = opendir($dir); //open a connection to the directory
$files = array();
if ($dp) {
while ($file = readdir($dp)) {
if (in_array($file, $skip)) continue;
if (is_dir("$dir$file")) {
$innerdp = opendir("$dir$file");
if ($innerdp) {
while ($innerfile = readdir($innerdp)) {
if (in_array($innerfile, $skip)) continue;
$arr = explode('.', $innerfile);
if (strtolower($arr[count($arr) - 1]) == 'pdf') {
$files[$file][] = $innerfile;
}
}
}
}
}
}
这部分将创建一个HTML表并显示所有适用的文件:
<table>
<? foreach ($files as $directory => $inner_files) { ?>
<tr>
<td>Folder: <?= $directory ?></td>
</tr>
<? foreach ($inner_files as $file) { ?>
<tr>
<td>File: <?= $directory ?>/<?= $file ?></td>
</tr>
<? } ?>
<? } ?>
</table>