Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php在列出目录后显示文件(第2部分)_Php_Arrays_Directory - Fatal编程技术网

Php在列出目录后显示文件(第2部分)

Php在列出目录后显示文件(第2部分),php,arrays,directory,Php,Arrays,Directory,这是我上一个问题的第二部分 现在,我已经将脚本完美地读取了目录,是否可以将文件列表的结果加载到一个新页面(如races.php)中,以“美化它” 下面是如何列出这些文件的 我真的很想让它显示在站点内部,而不是跳入apache服务器列表中,这样更方便用户 编辑 我想我在理论上要做的是,一旦脚本扫描目录,它就会将其放入一个名为$files的数组中,然后我会对所有文件夹进行foreach循环,但我现在遇到的问题是如何将$file传递到一个新页面?并使其显示文件夹中的内容:) 再次感谢,抱歉,这里的超级

这是我上一个问题的第二部分

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

下面是如何列出这些文件的 我真的很想让它显示在站点内部,而不是跳入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</>");
      }


     ?>

这个简单的函数可以为您做所有的事情

scandir(<path>);
scandir();

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

我在朋友的帮助下解决了问题。我希望其他人能利用这一点来帮助社区

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; ?>