【问题标题】:Populate HTML Table with Directory/Folder Contents使用目录/文件夹内容填充 HTML 表
【发布时间】:2012-09-13 07:23:59
【问题描述】:

在网站 HTML 表中,我需要使用位于网站特定目录中的 PDF 文件的名称填充该表。

例子:

表格左侧的年份:2010、2011、2012
表格顶部的月份:1 月、2 月、3 月

数据记录需要从站点根目录的结构化文件夹设置中提取:

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 文件的名称。

【问题讨论】:

  • 需要更多信息。什么样的环境? PHP之类的?

标签: php html html-table directory


【解决方案1】:

此 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>

【讨论】:

    猜你喜欢
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    相关资源
    最近更新 更多