【问题标题】:Php Show Files after listing directory (Part 2)列出目录后的 PHP 显示文件(第 2 部分)
【发布时间】:2014-07-22 03:01:47
【问题描述】:

这是我之前回答的问题的第二部分Php Browsing Multiple Directories

现在我已经让脚本完美地读取了目录,是否可以将文件列表的结果加载到新页面中,如races.php 以“美化”

这是列出文件的方式Example of Files being listed from server 我真的很想将它显示在网站内,而不是跳到 apache 服务器列表中,这样它对用户更友好。

编辑 我认为我在理论上试图做的是,一旦脚本扫描了目录,它就会将它放入一个名为 $files 的数组中,然后 foreach 循环所有文件夹,但我现在卡住的地方是我如何通过它$file 进入新页面?并使其显示文件夹内的内容:)

再次感谢和抱歉在这里尝试学习的超级新手!


脚本文件生成列表并单击每个文件内的文件:

<?php 

      $files = array();
     $dir = opendir('races/ob/'); 
      // $dir = opendir('races/ob/');

      while(false != ($file = readdir($dir))) {
              if(($file != ".") and ($file != "..") and ($file != "index.php")) {
                      $files[] = $file; // put in array.
              }   
      }

      natsort($files); // sort.

      // print.
      foreach($files as $file) {
              echo("<span class='txt-spacing'><a href='races/ob/$file'>$file</a> <br />\n</>");
      }


     ?>

【问题讨论】:

  • 所以你把脚本放在你想要的地方?
  • 对不起,我不明白你的意思?
  • 目前是什么阻止您将其放入races.php 或任何其他文件中?
  • 如何使链接正确加载?
  • 你需要对当前的问题给出更全面的解释。

标签: php arrays directory


【解决方案1】:

简单的功能就能帮你搞定

scandir(<path>);

它将为您提供目录中所有文件的列表。

【讨论】:

  • 我希望有一个例子来说明如何使用已经生成它们的链接来做到这一点,只是不确定如何让它们一起工作!
【解决方案2】:

在朋友的帮助下,我发现了我的问题。我希望其他人可以使用它来帮助社区。​​p>

1) 首先是列出 index.php 上的文件目录

2) 用户点击生成的文件夹后,进入races.php 并显示点击的文件夹中列出的文件的结果。

这是通过在 URL 中传递参数来完成的

index.php

<?php 

             $files = array();
             $dir = opendir('races/ob/'); 

             // $dir = opendir('races/ob/');
                        while(false != ($file = readdir($dir))) {
                       if(($file != ".") and ($file != "..") and ($file != "index.php")) {
                                        $files[] = $file; // put in array.
                                }   
                        }

                        natsort($files); // sort.

                        // print.
                        foreach($files as $file) {

                          $url  = "races/ob/$file";

                          $path = urlencode($url);

                           echo("<span class='txt-spacing'>
                             <a href='races.php?race=$path'>$file</a> <br />\n</>");
                        }


                       ?>

races.php

<?php

      $path = $_GET['race'];


      // right here, you need the path prefix

      $path = '/public_html' . urldecode($path); //SERVER PATH


      // above here you need it

      $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);

      $fileData = array();

      foreach($objects as $name => $object){

        $fileinfo = pathinfo($name);

        if (!is_dir($name) && isset($fileinfo['extension'])) {

         $file = $fileinfo['basename'];


         $fileData[] = $file;

        } 

      }

?>

          <?php foreach($fileData as $file): ?>

           <a target = '_blank' href="http://crpu.ca<?php echo urldecode($_GET['race'])                   . '/' . $file; ?>"><?php echo "$file<br>"; ?></a>
          <?php endforeach; ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 2023-04-02
    • 1970-01-01
    • 2016-10-20
    相关资源
    最近更新 更多