Simple example of file browser function implemented by PHP

  • 2021-12-21 04:16:42
  • OfStack

In this paper, the file browser function realized by PHP is described as an example. Share it for your reference, as follows:


<?php
if(isset($_GET['path'])){
  echo $path = $_SERVER['DOCUMENT_ROOT'].$_GET['path'];
  $pre_path = $_GET['path'];
}else{
  echo $path = $_SERVER['DOCUMENT_ROOT'];
  $pre_path = "";
}
?>
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  </head>
  <body>
    <table border="1">
      <thead>
        <tr>
          <td> Filename </td>
          <td> File size </td>
          <td> File type </td>
          <td> Modification time </td>
        </tr>
      <thead>
      <tbody>
        <?php
        $url_this = "http://".$_SERVER ['HTTP_HOST'].$_SERVER['PHP_SELF'];
        $handle = opendir($path);
        while($file=readdir($handle)){
          echo "<tr>";
          echo "<td>".$file."</td>";
          echo "<td>".filesize($path."/".$file)."</td>";
          if(filetype($path."/".$file)=="dir"){
            $next = $pre_path."/".$file;
            echo "<td><a href=\"$url_this?path=$next\">dir</a></td>";
          }else{
            echo "<td>".filetype($path."/".$file)."</td>";
          }
          echo "<td>".date("Y Year n Month t Day ",filemtime($path."/".$file))."</td>";
          echo "</tr>";
        }
        closedir($handle);
        ?>
      </tbody>
    </table>
  </body>
</body>

More readers interested in PHP can check the topics of this site: "php File Operation Summary", "PHP Directory Operation Skills Summary", "PHP Common Traversal Algorithms and Skills Summary", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary" and "PHP Network Programming Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: