Remove index.php and .htaccess from a list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vinicius

    Remove index.php and .htaccess from a list

    Hi,

    I have this script to list files in the same directory;

    <?
    $dir=opendir(". ");
    readdir($dir);
    readdir($dir);
    while ($conteudo = readdir($dir)) {
    echo "<a href=./$conteudo>$cont eudo</a><br>";
    }
    closedir($dir);
    ?>

    How can I remove from the list two files, index.php and .htaccess. I
    tried using 'if' but without success.

    Thanks...
  • Shawn Wilson

    #2
    Re: Remove index.php and .htaccess from a list

    Vinicius wrote:[color=blue]
    >
    > Hi,
    >
    > I have this script to list files in the same directory;
    >
    > <?
    > $dir=opendir(". ");
    > readdir($dir);
    > readdir($dir);
    > while ($conteudo = readdir($dir)) {
    > echo "<a href=./$conteudo>$cont eudo</a><br>";
    > }
    > closedir($dir);
    > ?>
    >
    > How can I remove from the list two files, index.php and .htaccess. I
    > tried using 'if' but without success.[/color]

    I think this should work, though it's untested. I often forget a \ or ", etc.

    <?PHP // <-- use "<?PHP" instead of "<?"
    $dir=@opendir(" ."); //Open directory, suppressing any errors
    if ($dir): //only try to readdir if you opened it

    $arrBannedFiles = array(".", "..", "index.php" , ".htaccess" );

    while ($conteudo = readdir($dir)) {
    // If file isn't banned, print it, with quotes,
    // urlencode the href and print any special characters
    // in the text (and add a newline for source code neatness)
    if (!in_array($con teudo, $arrBannedFiles ))
    echo "<a
    href=\"./".urlencode($co nteudo)."\">".h tmlentities($co nteudo)."</a><br>\n";
    }
    closedir($dir);
    else: // If directory couldn't be opened
    echo "Couldn't open dir";
    endif;
    ?>

    Regards,
    Shawn

    --
    Shawn Wilson
    shawn@glassgian t.com

    Comment

    • Michael Fuhr

      #3
      Re: Remove index.php and .htaccess from a list

      vinicius@sordid o.com.br (Vinicius) writes:
      [color=blue]
      > I have this script to list files in the same directory;
      >
      > <?
      > $dir=opendir(". ");
      > readdir($dir);
      > readdir($dir);
      > while ($conteudo = readdir($dir)) {
      > echo "<a href=./$conteudo>$cont eudo</a><br>";
      > }
      > closedir($dir);
      > ?>
      >
      > How can I remove from the list two files, index.php and .htaccess. I
      > tried using 'if' but without success.[/color]

      What exactly did you try? We can't tell you why "if" didn't work
      if you don't show us how you were using it.

      --
      Michael Fuhr

      Comment

      • FLEB

        #4
        Re: Remove index.php and .htaccess from a list

        Regarding this well-known quote, often attributed to Vinicius's famous "29 Dec 2003 07:34:57 -0800" speech:
        [color=blue]
        > Hi,
        >
        > I have this script to list files in the same directory;
        >
        > <?
        > $dir=opendir(". ");
        > readdir($dir);
        > readdir($dir);
        > while ($conteudo = readdir($dir)) {
        > echo "<a href=./$conteudo>$cont eudo</a><br>";
        > }
        > closedir($dir);
        > ?>
        >
        > How can I remove from the list two files, index.php and .htaccess. I
        > tried using 'if' but without success.
        >
        > Thanks...[/color]

        Here's my solution. Untested, but it should work...
        assume that the $dir varible has already been set --

        <?php

        $prohibited = array( // An array, with Regexp patterns to match prohibited files
        '/^\./', // No *NIX hidden files... this also drops .htaccess, as well as ".", ".." entries
        '/^htaccess\.txt$/', // Some servers call it "htaccess.t xt"
        '/^index\.php$/'); // Also drop your index.php file

        $listing = scandir($dir);

        foreach($prohib ited as $thisProhibited )
        $listing = preg_grep($this Prohibited, $listing, PREG_GREP_INVER T);
        // see http://www.php.net/manual/en/function.preg-grep.php Note 1
        // I'm not sure if PREG_GREP_INVER T is fully supported. YMMV.
        }

        foreach($listin g as $thisDirItem)
        {
        echo "<a href=\"$thisDir Item\">$thisDir Item</a><br>";
        // spit out the list, minus everything we removed above
        }

        ?>



        --
        -- Rudy Fleminger
        -- sp@mmers.and.ev il.ones.will.bo w-down-to.us
        (put "Hey!" in the Subject line for priority processing!)
        -- http://www.pixelsaredead.com

        Comment

        • FLEB

          #5
          Re: Remove index.php and .htaccess from a list

          Regarding this well-known quote, often attributed to FLEB's famous "Mon, 29
          Dec 2003 15:16:04 -0500" speech:
          [color=blue]
          > Regarding this well-known quote, often attributed to Vinicius's famous "29 Dec 2003 07:34:57 -0800" speech:
          >[color=green]
          >> Hi,
          >>
          >> I have this script to list files in the same directory;
          >>
          >> <?
          >> $dir=opendir(". ");
          >> readdir($dir);
          >> readdir($dir);
          >> while ($conteudo = readdir($dir)) {
          >> echo "<a href=./$conteudo>$cont eudo</a><br>";
          >> }
          >> closedir($dir);
          >> ?>
          >>
          >> How can I remove from the list two files, index.php and .htaccess. I
          >> tried using 'if' but without success.
          >>
          >> Thanks...[/color]
          >
          > Here's my solution. Untested, but it should work...
          > assume that the $dir varible has already been set --[/color]

          If you've got the word-wrap blues, you can download that as a text file
          from:




          --
          -- Rudy Fleminger
          -- sp@mmers.and.ev il.ones.will.bo w-down-to.us
          (put "Hey!" in the Subject line for priority processing!)
          -- http://www.pixelsaredead.com

          Comment

          • CountScubula

            #6
            Re: Remove index.php and .htaccess from a list

            You should not display any file that begins with a '.' this signifies a
            hiddden file


            Mike Bradley
            http://gzen.myhq.info -- free online php tools


            Comment

            Working...