Excluding index.php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gcclinux
Forum Newbie
Posts: 6
Joined: Thu May 03, 2007 6:11 am

Excluding index.php

Post by gcclinux »

JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello everyone,

I have a bit of code that list all files and directories in the current directory and it does almost exactly what I want apart from the fact that I would like to exclude the index.php file it self from being listed.

This is the current code i've written. (with help)

Code: Select all

<?php
echo '<h1><center> "Personal Photo Directory" </h1>';

function dir_to_array ($dir, $pattern = '.*' )
{
  $contents = array();
  if ($dh = opendir($dir))
  {
    while (($file = readdir($dh)) !== false)
    {
      if ( preg_match('/'.$pattern.'/',$file)&& $file!="." && $file !=".." )
          $contents[] = $file;
    }
    closedir($dh);
  }

  sort($contents);
  return $contents;
}

# Get the list of files
$dir_list = dir_to_array('./','.*.*');

# Print them out
foreach ($dir_list as $filename)
  echo '<br><center> <a href="./'.$filename.'">'.$filename.'</a>';
?>
<br><br>
<a>----------------------------------------</a><br><br>
As you can see it does what it is suppose to do, so now all i need is to excluded it self, any help will be greatly appreciated.

Many Thanks

Ricardo


JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Do you not know where to add the logic to exclude values?
gcclinux
Forum Newbie
Posts: 6
Joined: Thu May 03, 2007 6:11 am

Post by gcclinux »

JayBird I do apologies for the inconvenience that I have caused, I will remember to post it correctly in the future.
feyd I have tried a number of things but I do not seem to get it to work, I would appreciate your help, or anyone that knows.
Many Thanks

Ricardo
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Code: Select all

if ( preg_match('/'.$pattern.'/',$file)&& $file!="." && $file !=".." && $file != "index.php")
gcclinux
Forum Newbie
Posts: 6
Joined: Thu May 03, 2007 6:11 am

Post by gcclinux »

shiznatix wrote:

Code: Select all

if ( preg_match('/'.$pattern.'/',$file)&& $file!="." && $file !=".." && $file != "index.php")
Thank you so much. I should have paid more attention and I would have figured it out!

Once again thank you.

Ricardo
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

gcclinux wrote:Thank you so much. I should have paid more attention and I would have figured it out!

Once again thank you.

Ricardo
don't worry baby, I'm here for you. Just keep typing to me so softly.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

shiznatix wrote:
gcclinux wrote:Thank you so much. I should have paid more attention and I would have figured it out!

Once again thank you.

Ricardo
don't worry baby, I'm here for you. Just keep typing to me so softly.
Uhhh.. sketchy talk with long time forum members = weird, but okay.

Sketchy talk with new forum members = NO! Bad shiznatix! :lol:
Post Reply