473,800 Members | 2,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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...
Jul 17 '05 #1
5 5270
Vinicius wrote:

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.


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
sh***@glassgian t.com
http://www.glassgiant.com
Jul 17 '05 #2
vi******@sordid o.com.br (Vinicius) writes:
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.


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
http://www.fuhr.org/~mfuhr/
Jul 17 '05 #3
Regarding this well-known quote, often attributed to Vinicius's famous "29 Dec 2003 07:34:57 -0800" speech:
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...


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
Jul 17 '05 #4
Regarding this well-known quote, often attributed to FLEB's famous "Mon, 29
Dec 2003 15:16:04 -0500" speech:
Regarding this well-known quote, often attributed to Vinicius's famous "29 Dec 2003 07:34:57 -0800" speech:
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...


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


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

http://funhappythings.pixelsaredead....ohibit.php.txt
--
-- 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
Jul 17 '05 #5
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
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6329
by: Lab309 | last post by:
The problem I'm trying to solve is as follows: The website has two subdirectories: /ordinary and /phpstuff. Users typing hostname/ordinary get the file index.htm by default, and this works fine. Users typing hostname/phpstuff don't get anything because there is no index.htm there, only index.php. What can I put in an index.htm file that'll invoke the index.php file? Or is there a setting in the Apache 2.0 server I've overlooked? ...
2
7754
by: frizzle | last post by:
Hi there I have a site in which all pages ARE php-pages, but they're called/manipulated with htaccess. All files appear as a html-file to surfers. Sometimes i get the PHPSESSID declared in the url. I want to avoid this from caching in the browsers history. is there a way to detect if PHPSESSID is set, and if
4
2453
by: kurtk | last post by:
I have install Apache and php5. I can execute php with no problem. However, I would like index.php to be recognized and executed the way that index.html is. That is, I want to be able to type www.mysite.com in the browser and not have to always type www.mysite.com/index.php. Is that an .htaccess change of some sort? I not an Apache expert.
8
2552
by: Rob | last post by:
I'm sure this must be something incredibly simple but I just can't see what the problem is. I have some webspace which is accessed along the following lines http://site.address.com/subdir/ but instead of my browser (either Firefox or IE) picking up the index.html file and displaying it I just get a list of all the files in that subdirectory which of course includes the file index.html. Any
9
15347
by: Merlin | last post by:
Hi, My code below doesn't work does anyone have any pointers? All my controls are programically added. Dim i As Int16 For i = 0 To Me.Controls.Count - 1 If Me.Controls(i).Name <> "TheOneIWantToKeep" Then Me.Controls.RemoveAt(i) End If
14
3050
by: oLgAa25 | last post by:
Hello all, I am back I have this question everything compiles fine, but I just need to check for an index that is more than the array size. Although it says out of bound, but it is still deleting the last element. Can someone help please Thank you all Olga I just wanted to test my function,so don't bother with main, and print
2
3941
by: Kela | last post by:
An interesting problem: I have a ListView with LabelEdit set to TRUE. When I change the label, I want to make some decisions as to whether the ListViewItem (that's just been edited) should stay in the ListView or not. The natural place to inspect this is in the AfterLabelEdit event... If the decision is to take the item out, I used the ListViewItem.Remove(). However - check this out: A) say that the item's index is N (indices are...
10
18079
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I wanted to go through each entry(?) of ArrayList and remove some particular entry. So I tried following but it throws exception at runtime: foreach (myEntry entry in myArrayList) { // do something... if (entry.fieldA == 0)
1
1295
by: pipetawer | last post by:
Greetings, I am having some kind of trouble with some code. I have the following object using System; using System.Collections; namespace ImageServerCleanup { public class DiffList : CollectionBase
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10276
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10035
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9090
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7580
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6813
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.