首页 > 解决方案 > PHP和HTML制作漂亮的交互式文件显示页面

问题描述

我找到了一个很好的演示代码,我能够使用我正在尝试做的事情。但最终产品相当丑陋,并且不允许我查看文件。就“不错”的部分而言,我觉得我应该看起来更像是面向 jquery 的东西?

当我单击显示文件的链接时,我收到一个 url not found 错误。文件路径不正确...

这是不正确的,脚本当前尝试导航到的内容:

http://server/var/www/html/reports/1/Doe_John/2019-04-01/Run_2_Report.pdf

这是正确的,可以在浏览器中显示文件:

http://server/reports/1/Doe_John/2019-04-01/Run_2_Report.pdf

是否有一种直接的方法可以“减去”导致问题的额外文件路径部分?我必须添加 {$_SERVER['DOCUMENT_ROOT']} 部分以使其找到文件,但这似乎是现在导致问题的原因。

我还想按日期对文件进行排序,但这样做不正确。日期按月份字母顺序排序。这可以只用 HTML 来完成,还是我应该再次查看 Jquery 之类的东西?

以结构显示所有 pdf 文件的 PHP:

<?PHP
  // Original PHP code by Chirp Internet: www.chirp.com.au
  // Please acknowledge use of this code by including this header.

  //Test User Vars
  $region = "1";
  $first_name = "John";
  $last_name = "Doe";

  function getFileList($dir, $recurse = FALSE)
  {
    // array to hold return value
    $retval = array();

    // add trailing slash if missing
    if(substr($dir, -1) != "/") $dir .= "/";

    // open pointer to directory and read list of files
    $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
    while(false !== ($entry = $d->read())) {
      // skip hidden files
      if($entry[0] == ".") continue;
      if(is_dir("$dir$entry")) {
        $retval[] = array(
          "name" => "$dir$entry/",
          "type" => filetype("$dir$entry"),
          "size" => 0,
          "lastmod" => filemtime("$dir$entry")
        );
        if($recurse && is_readable("{$dir}{$entry}/")) {
          $retval = array_merge($retval, getFileList("{$dir}{$entry}/", TRUE));
        }
      } elseif(is_readable("$dir$entry")) {
        $retval[] = array(
          "name" => "$dir$entry",
          "type" => mime_content_type("$dir$entry"),
          "size" => filesize("$dir$entry"),
          "lastmod" => filemtime("$dir$entry")
        );
      }
    }
    $d->close();

    return $retval;
  }
?>

<h1>List PDF files with links</h1>

<table class="collapse" border="1">
<thead>
<tr><th>Name</th><th>Type</th><th>Size</th><th>Last Modified</th></tr>
</thead>
<tbody>
<?PHP
  $dirlist = getFileList("{$_SERVER['DOCUMENT_ROOT']}/reports/{$region}/{$last_name}_{$first_name}/", TRUE);
  foreach($dirlist as $file) {
    if($file['type'] != "application/pdf") continue;
    echo "<tr>\n";
    echo "<td><a href=\"{$file['name']}\">",basename($file['name']),"</a></td>\n";
    echo "<td>{$file['type']}</td>\n";
    echo "<td>{$file['size']}</td>\n";
    echo "<td>",date('r', $file['lastmod']),"</td>\n";
    echo "</tr>\n";
  }
?>
</tbody>
</table>

标签: phphtml

解决方案


第二个链接有效,因为/var/www/html/可能是您服务器的 DocumentRoot,如果您使用的是 Apache,它是您应用程序的根目录。


推荐阅读