Jump to content

How to hide file extensions using $handle = opendir


lorne17

Recommended Posts

Hello there,

 

I'm new to PHP and i have set up a file to allow for the user to see a list of files in a specific directory.

 

I used this code and it works great.  No issues:

<?php
if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
  {
          	$thelist .= '<a href="'.$file.'">'.$file.'</a>'.'<br>';
          }
       }
  closedir($handle);
  }
?>
<p style="font-weight: bold; font-size: 10pt">List of files:</p>
<p style="font-weight: bold; font-size: 10pt; color: #560D1C"><?=$thelist?></p>

 

Now what I want to do is specifically hide files that are in this directory.  When the user looks at the list the files .htaccess, .htpsswd, index.php.  The first two files are automatically created when I save index.php.  So what I want is to hide those three including the index.php (take note that the page with this list is the index.php).

 

Can anyone give suggestions??  Thanks,

 

Lorne

 

<?php

if ($handle = opendir('.')) {

  $blacklist = array('.', '..', 'index.php', '.htaccess', '.htpswwd');

  while (false !== ($file = readdir($handle)))

      {

          if (!in_array($file, $blacklist))

  {

          $thelist .= '<a href="'.$file.'">'.$file.'</a>'.'<br>';

          }

      }

  closedir($handle);

  }

?>

<p style="font-weight: bold; font-size: 10pt">List of files:</p>

<p style="font-weight: bold; font-size: 10pt; color: #560D1C"><?=$thelist?></p>

Change

while (false !== ($file = readdir($handle)))
    {
        if ($file != "." && $file != "..")

to

$ignore_files = array('.', '..', '.htaccess', '.htpsswd', 'index.php');

while (false !== ($file = readdir($handle)))
    {
        if (!in_array($file, $ignore_files))

 

EDIT: too slow ;)/b]

That worked great, Thanks :) 

 

...here's the final working code for reference...

<?php
if ($handle = opendir('.')) {
   $ignore_files = array('.', '..', '.htaccess', '.htpasswd', 'index.php');
   while (false !== ($file = readdir($handle)))
      {
          if (!in_array($file, $ignore_files))
     {
             $thelist .= '<a href="'.$file.'">'.$file.'</a>'.'<br>';
          }
       }
  closedir($handle);
  }
?>
<p style="font-weight: bold; font-size: 10pt">List of files:</p>
<p style="font-weight: bold; font-size: 10pt; color: #560D1C"><?=$thelist?></p>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.