PDA

View Full Version : directory script



insanemonkey
11-01-2007, 07:56 PM
I haveing some issues..

I have a directory script and it shows
.
..
before the files..

how do i get rid of it..




<?
include("../include/session.php");
?>

<?php
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {

echo "$file";
}
closedir($dh);
}
}
?>


just need to get rid of
.
..

thanks

boogyman
11-01-2007, 08:49 PM
../ indicates the file above the current directory.

if the include directory is underneath the document root or accessible to the web (http://mydomain.com) you have 2 options. you can use the absolute path (http://mydomain.com/folder/file)
or a relative absolute path (/folder/file)... the first forward slash is an indication to start at the document root.

if the include file is above the document root well it would be more tricky. because you need to edit some other scripts on your server. we can tackle that if this is the case... PS we would need the OS Type (Linux / Unix / Windows)

insanemonkey
11-01-2007, 09:09 PM
I think its windows...

Twey
11-01-2007, 09:16 PM
if (is_dir($dir))
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false)
if (strpos($file, '.') !== 0)
echo $file;
closedir($dh);
}
}

djr33
11-01-2007, 09:18 PM
Though that's the most efficient in this case, you should learn to make a habit of including this line:
if ($f=='.'||$f== '..') { continue; }
(at that start of the loop)

Twey
11-01-2007, 09:25 PM
The equivalent would be:
if (is_dir($dir))
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (strpos($file, '.') === 0) continue;
echo $file;
}
closedir($dh);
}djr33, you even include braces when you neglect indentation? :-\

djr33
11-01-2007, 09:33 PM
I don't feel like wasting space with indentation, as it's a simple and notable command-- skip . and .., not some complex if and reaction.
I don't like omitting brackets because it guides the eye-- if they aren't there, your eye may continue to find the close bracket (as the first, or, rather, lack thereof, may not be noticed when skimming because it sorta blends with the close ')').

By the way, I wasn't rebutting your reply, but just telling insanemonkey to use that. It's a good habit, unless you want to completely skip all directories.
In most cases, one wants to go through everything and of course skip the . and .. directories, but do something with files and do something else with directories; but skip the . and .. dirs. This will end with some weird errors, too, so it's just a basic part of the setup.

The exception, obviously, is if you just want to skip all directories; in that case, just use the above line.

insanemonkey
11-01-2007, 09:36 PM
thankyou you forgot to put some '{' in there but i got it thankyou..

Twey
11-01-2007, 09:51 PM
I don't feel like wasting space with indentation, as it's a simple and notable command-- skip . and .., not some complex if and reaction.Fair.
I don't like omitting brackets because it guides the eye-- if they aren't there, your eye may continue to find the close bracket (as the first, or, rather, lack thereof, may not be noticed when skimming because it sorta blends with the close ')').Not at all. The indentation (or lack thereof) shows clearly that the conditionally-executed block doesn't extend to the next line.
The exception, obviously, is if you just want to skip all directories; in that case, just use the above line.What? My code, at least, had nothing to do with skipping all directories...

djr33
11-01-2007, 09:53 PM
Huh?
if (strpos($file, '.') === 0) continue;
What else DOES that do?


As for indentation, that's fair enough, but I find the brackets still helpful if skimming quickly (as if to find a section of the code). When looking at each line, lack of brackets is fine.
Also, the real reason for including it is to be absolutely correct. I see omitting the brackets as a shortcut, just like omitting the ; in javascript. I prefer having these, just to be sure that's not the problem if/when debugging. If I'm writing quickly, I may omit them. (Or to minimize the characters in a code, if there's some reason.)

insanemonkey
11-01-2007, 10:12 PM
well without the brachets it didnt work..

you guys are confusing me...

Twey
11-01-2007, 10:31 PM
if (strpos($file, '.') === 0) continue;
What else DOES that do?Omits any UNIX hidden files (ones that start with a .). This includes the directories "." and "..".
I find the brackets still helpful if skimming quickly (as if to find a section of the code).Hm, I always rely on indentation for this. Otherwise one has to keep track of every single brace in order to know exactly how many levels deep one is.
Also, the real reason for including it is to be absolutely correct. I see omitting the brackets as a shortcut, just like omitting the ; in javascript.But omitting a semicolon in Javascript is bad because it can lead to the code looking like it does something completely different to what it actually does:
var a = 3,
b = 1 + a
(b - 4) && doSomething()... and is inserted anyway. Omitting a brace isn't nearly so ambiguous, especially with proper indentation, and doesn't make extra work for the compiler/interpreter.
thankyou you forgot to put some '{' in there but i got it thankyou..No, I actually accidentally left one in (the last one, I was editing your code, and it was outside the visible area of my textbox).
well without the brachets it didnt work..

you guys are confusing me...Don't mind us, we just went off on a tangent about coding style :)

insanemonkey
11-01-2007, 10:51 PM
omg i understood that... thankyou... i learned that in math.. lol