Skip to content
WJunction - Webmaster Forum

php: Hide *. php from the listing of files.

Status
Not open for further replies.

Daniel

Community Legend
16,012
2009
3,075
Hello, i'm sitting here and playing and trying to make a "pool" to a website in php.
What I'm going is to show the contents of a folder using php. So far I have managed to list all files in selected folder using:

PHP:
<?php
 if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
      {
              $thelist .= '<a href="'.$file.'">&laquo;'.$file.'&raquo;  <br></a>';
          }
       }
  closedir($handle);
  }
?>
It is so far a list good enough for my purposes, except for one thing. I want to hide files with specifix extensions, specifically *. php.
I'm struggling to really figure this out, because I am completely blank on the field....
 

5 comments

PHP:
<?php

$list = array(); //define an empty array

if ($handle = opendir('.'))
{
	while (false !== ($file = readdir($handle)))
	{
		if ($file != "." && $file != "..")
		{
			array_push($list, $file);
		}
	}
closedir($handle);
sort($list);
}

foreach ($list as $file)
{
	$ext = substr(strrchr($file, '.'), 1);
	if ($ext !== 'php')
	{
		echo '<a href="'.$file.'">&laquo;'.$file.'&raquo; </a><br>';
	}
}

?>
 
Or you can shorten up the code to have the same effect :)

PHP:
<?php
$list = scandir('.');
foreach ($list as $file){
    $ext = pathinfo($file,PATHINFO_EXTENSION);
    if( ($file != ".") || ($file != "..") || ($ext != 'php') ){
        echo '<a href="'.$file.'">&laquo;'.$file.'&raquo; </a><br>';
    }
}
?>
 
Yep.

Add more extentions to hide as such
PHP:
($ext != 'cgi') || ($ext != 'pl') || ($ext != 'html') || ($ext != 'htm')

This will hide cgi, pl, html and html files aswell.

If you want to store these files list to a array,

PHP:
<?php
$myList = array(); //Blank array to store non PHP files
$list = scandir('.');
foreach ($list as $file){
    $ext = pathinfo($file,PATHINFO_EXTENSION);
    if( ($file != ".") || ($file != "..") || ($ext != 'php') ){
        $myList[] = $file;
    }
}
?>
 
Status
Not open for further replies.

About the author

D
Community Legend · Joined
Gaming
16,012
Messages
3,075
Reactions
113
Points
Grow your business

Advertise on WJunction

Connect with an established international community of webmasters, hosting providers, developers and online entrepreneurs.

  • Premium & Premium+ Sticky
  • Dedicated section banners
  • Site-wide banner advertising
Explore advertising options
Back
Top Bottom