Carlos Oulman
Carlos Oulman

Reputation: 21

Hide PHP Files in Folder Directory

I am creating a page that I can upload files to and show them to the user in a neat looking web page. I am using the following code to get the results that I want. Everything works great however I do not wish to show the index.php file in the list. How do I get it to hide this?

<!doctype html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Chase Andrews Show - Promos</title>
  <link rel="stylesheet" href="/common/dir-style.css">
  <script src="/common/sorttable.js"></script>
</head>

<body>

  <div id="container">

    <h1>Chase Andrews Show - Promos</h1>

    <table class="sortable">
      <thead>
        <tr>
          <th>Filename</th>
          <th>Type</th>
          <th>Size <small>(bytes)</small></th>
          <th>Date Modified</th>
        </tr>
      </thead>
      <tbody>
      <?php
        // Opens directory
        $myDirectory=opendir(".");

        // Gets each entry
        while($entryName=readdir($myDirectory)) {
          $dirArray[]=$entryName;
        }

        // Finds extensions of files
        function findexts ($filename) {
          $filename=strtolower($filename);
          $exts=split("[/\\.]", $filename);
          $n=count($exts)-1;
          $exts=$exts[$n];
          return $exts;
        }

        // Closes directory
        closedir($myDirectory);

        // Counts elements in array
        $indexCount=count($dirArray);

        // Sorts files
        sort($dirArray);

        // Loops through the array of files
        for($index=0; $index < $indexCount; $index++) {

          // Allows ./?hidden to show hidden files
          if($_SERVER['QUERY_STRING']=="hidden")
          {$hide="";
          $ahref="./";
          $atext="Hide";}
          else
          {$hide=".";
          $ahref="./?hidden";
          $atext="Show";}
          if(substr("$dirArray[$index]", 0, 1) != $hide) {

          // Gets File Names
          $name=$dirArray[$index];
          $namehref=$dirArray[$index];

          // Gets Extensions 
          $extn=findexts($dirArray[$index]); 

          // Gets file size 
          $size=number_format(filesize($dirArray[$index]));

          // Gets Date Modified Data
          $modtime=date("M j Y g:i A", filemtime($dirArray[$index]));
          $timekey=date("YmdHis", filemtime($dirArray[$index]));

          // Prettifies File Types, add more to suit your needs.
          switch ($extn){
            case "png": $extn="PNG Image"; break;
            case "jpg": $extn="JPEG Image"; break;
            case "svg": $extn="SVG Image"; break;
            case "gif": $extn="GIF Image"; break;
            case "ico": $extn="Windows Icon"; break;

            case "txt": $extn="Text File"; break;
            case "log": $extn="Log File"; break;
            case "htm": $extn="HTML File"; break;
            case "php": $extn="PHP Script"; break;
            case "js": $extn="Javascript"; break;
            case "css": $extn="Stylesheet"; break;
            case "pdf": $extn="PDF Document"; break;

            case "zip": $extn="ZIP Archive"; break;
            case "bak": $extn="Backup File"; break;

            default: $extn=strtoupper($extn)." File"; break;
          }

          // Separates directories
          if(is_dir($dirArray[$index])) {
            $extn="&lt;Directory&gt;"; 
            $size="&lt;Directory&gt;"; 
            $class="dir";
          } else {
            $class="file";
          }

          // Cleans up . and .. directories 
          if($name=="."){$name=". (Current Directory)"; $extn="&lt;System Dir&gt;";}
          if($name==".."){$name=".. (Parent Directory)"; $extn="&lt;System Dir&gt;";}

          // Print 'em
          print("
          <tr class='$class'>
            <td><a href='./$namehref'>$name</a></td>
            <td><a href='./$namehref'>$extn</a></td>
            <td><a href='./$namehref'>$size</a></td>
            <td sorttable_customkey='$timekey'><a href='./$namehref'>$modtime</a></td>
          </tr>");
          }
        }
      ?>
      </tbody>
    </table>

    <h2><?php print("<a href='$ahref'>$atext hidden files</a>"); ?></h2>

  </div>

</body>

</html>

Upvotes: 0

Views: 2456

Answers (2)

hurnhu
hurnhu

Reputation: 936

If I understood the code and question correctly. You should just be able to have an if statement in your while to skip any file named "index.php"

while($entryName=readdir($myDirectory)) {
          if($entryName != 'index.php'){
          $dirArray[]=$entryName;
          }
        }

Upvotes: 0

elixenide
elixenide

Reputation: 44851

Just don't add it to your array. Change

    // Gets each entry
    while($entryName=readdir($myDirectory)) {
      $dirArray[]=$entryName;
    }

To

    // Gets each entry
    while($entryName=readdir($myDirectory)) {
      if ('index.php' !== $entryName) {
        $dirArray[]=$entryName;
      }
    }

Also note, as explained in the documentation, your loop is incorrect. From the docs:

Please note the fashion in which readdir()'s return value is checked in the examples below. We are explicitly testing whether the return value is identical to (equal to and of the same type as--see Comparison Operators for more information) FALSE since otherwise, any directory entry whose name evaluates to FALSE will stop the loop (e.g. a directory named "0").

So, really, your loop should be:

    // Gets each entry
    while(false !== ($entryName=readdir($myDirectory))) {
      if ('index.php' !== $entryName) {
        $dirArray[]=$entryName;
      }
    }

Upvotes: 1

Related Questions