Jump to content

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>

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.