简体   繁体   中英

Php Show Files after listing directory (Part 2)

This is a part two to my previous question that got answered Php Browsing Multiple Directories

Now that I got the script reading the directories perfectly, Is it possible to load the results of the file listings into a new page like races.php to "pretty it up"

Here is how it's listing the files Example of Files being listed from server I would really like to have that be displayed inside of the site instead of jumping out into a apache server listing so its more user friendly.

EDIT I think what I'm trying to do in theory that is once the script scans the dir it puts it into an array called $files I then foreach loop it for all of the folders but where I get stuck now is how do I pass that $file into a new page ? and make it show the contents inside the folder :)

Thanks again and sorry super newbie here trying to learn!


Script File to generate list and click to files inside of each:

<?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</>");
      }


     ?>

The simple function can do all for you

scandir(<path>);

It will give you list of all files in the directory.

I figured out my problem with some help from a friend. I hope others can use this to help out the community.

1) First thing is to list A directory of files on the index.php

2) Once the user clicks the generated folder, go into races.php and display the the results of the files listed inside the folder clicked.

Here is how it's done by passing a parameter in the 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; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM