【问题标题】:Listing Directories: Opendir()列出目录:Opendir()
【发布时间】:2012-10-15 18:39:32
【问题描述】:

我正在编写一个小脚本,我想列出目录的内容,将它们制作成超链接,然后编辑这些超链接以使其看起来很漂亮(即不显示丑陋的超长路径名),然后限制number 文件回显到浏览器。另外,我只需要回显最新的文件。

我正在考虑使用这个:

<?php
 $path = "/full/path/to/files";
 if ($handle = opendir($path)) {
   while (false !== ($file = readdir($handle)))
          {
                  if ($file != "." && $file != "..")
          {
                          $files .= '<a href="'.$file.'">'.$file.'</a>';
                  }
           }
  closedir($handle);
  }
?>

或者这个:

<?php
$sub = ($_GET['dir']);
$path = 'enter/your/directory/here/';
$path = $path . "$sub";
$dh = opendir($path);
$i=1;
while (($file = readdir($dh)) !== false) {
        if($file != "." && $file != "..") {
                        if (substr($file, -4, -3) =="."){
                        echo "$i. $file <br />";
                        }else{                  
                echo "$i. <a href='?dir=$sub/$file'>$file</a><br />";
                  }
                $i++;
        }
}
closedir($dh);
?>

但我不想像这样列出文件:

C:/example/example2/Hello.pdf

我想编辑变量。那可能吗?让它说像“你好”这样简单的东西。

我也想限制列出的文件数量。例如:只列出前 5 个文件,或最后 5 个文件等。是否有一个函数或某种参数?

感谢任何帮助或朝着正确的方向前进。谢谢

【问题讨论】:

    标签: php list opendir


    【解决方案1】:

    我正在使用手机,因此提供代码示例会很困难。为什么不遍历目录,将文件名存储在数组中,以绝对路径作为该键的值?

    编辑:您可以使用basename 来帮助您完成此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      相关资源
      最近更新 更多