i want to list down files and folders of a directory.
Moderator: General Moderators
- itsmani1
- Forum Regular
- Posts: 791
- Joined: Mon Sep 29, 2003 2:26 am
- Location: Islamabad Pakistan
- Contact:
i want to list down files and folders of a directory.
I want to list down all files and folder of a specfic directory, for example my direcoty structure is something like this
root
|_folder 1
| |_______ index.html
| |_______ xyz.html
|_abc.html
Now 1st i want to access root, then goto foder1 and its files and abc.html in root directoy.
please help me, how i can do so?
thanks.
root
|_folder 1
| |_______ index.html
| |_______ xyz.html
|_abc.html
Now 1st i want to access root, then goto foder1 and its files and abc.html in root directoy.
please help me, how i can do so?
thanks.
-
SpiderMonkey
- Forum Commoner
- Posts: 85
- Joined: Fri May 05, 2006 4:48 am
-
SpiderMonkey
- Forum Commoner
- Posts: 85
- Joined: Fri May 05, 2006 4:48 am
OK, here is how I did it:
This will loop through all the filenames in $file
Bear in mind though, that the first to filenames will always be . and ..
As for the tree bit - use to pick out the subdirectories and call your function recursively.
Code: Select all
$dir=opendir("directory");
while ($file=readdir($dir)) {
...
}Bear in mind though, that the first to filenames will always be . and ..
As for the tree bit - use
Code: Select all
is_dir($file)This is the wrong way to do it.
You should do it like this:
The reason (from the php.net site):
Please note the fashion in which readdir()'s return value is checked in the examples below. We are explicitly testing whether the return value is identical to (equal to and of the same type as--see Comparison Operators for more information) FALSE since otherwise, any directory entry whose name evaluates to FALSE will stop the loop (e.g. a directory named "0").
In SpiderMonkey's way, a file named '0' for instance, will end the loop.
FYI: In PHP 5 you could do exactly the same thing with this code:
You should do it like this:
Code: Select all
<?php
$dir = "/tmp";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh)))
{
$files[] = $filename;
}
?>Please note the fashion in which readdir()'s return value is checked in the examples below. We are explicitly testing whether the return value is identical to (equal to and of the same type as--see Comparison Operators for more information) FALSE since otherwise, any directory entry whose name evaluates to FALSE will stop the loop (e.g. a directory named "0").
In SpiderMonkey's way, a file named '0' for instance, will end the loop.
FYI: In PHP 5 you could do exactly the same thing with this code:
Code: Select all
<?php
$dir = '/tmp';
$files = scandir($dir);
?>-
SpiderMonkey
- Forum Commoner
- Posts: 85
- Joined: Fri May 05, 2006 4:48 am
No it isn't.Oren wrote:This is the wrong way to do it.
Its best to avoid giving files such stupid names anyway, even if you aren't using this function.In SpiderMonkey's way, a file named '0' for instance, will end the loop.
Which is why I asked which version he was using first...FYI: In PHP 5 you could do exactly the same thing with this code:Code: Select all
<?php $dir = '/tmp'; $files = scandir($dir); ?>
Yes it is.SpiderMonkey wrote:No it isn't.
That's your argument?SpiderMonkey wrote:Its best to avoid giving files such stupid names anyway, even if you aren't using this function.
Dude... I don't want to insult, but you should listen to people which are more experienced than you.
What if I have some kind of image upload site and one of the members would like to upload a directory named '0'? I've seen a lot of strange names... Including '0'.
Sometimes you even make your script create such a directory to keep things ordered. E.g for each new member you create a new directory: The first member will get a directory named '0', the second member will get a directory named '1' and so on.
-
SpiderMonkey
- Forum Commoner
- Posts: 85
- Joined: Fri May 05, 2006 4:48 am
You don't want to insult? Well that ship sailed when you told me that some code which i am *currently using on a live* website is 'wrong', then cut and paste something from php.net, and THEN claimed to be 'more experienced' than me.Oren wrote: Dude... I don't want to insult, but you should listen to people which are more experienced than you.
What if I have some kind of image upload site and one of the members would like to upload a directory named '0'? I've seen a lot of strange names... Including '0'.
Sometimes you even make your script create such a directory to keep things ordered. E.g for each new member you create a new directory: The first member will get a directory named '0', the second member will get a directory named '1' and so on.
I am well aware of the issue you mentioned, I simply didn't bother with it because it is so unlikely to occur with sensibly named fileds. Given that users aren't going to be writing files to your web server without you being able to filter out idiotic filenames, I didn't consider it was an issue.
You ought to learn some respect.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Just a friendly note to SpiderMonkey and Oren:
Ta muchly,
Mac
Please keep it civil.[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.3 wrote:13. A wide variety of opinions may be expressed on this public discussion forum. Respect positions which differ from your own. We like to see a vigorous intellectual discussion which proceeds politely and addresses the technical merits of different positions. Do not expect that anyone will alter their opinion of a topic regardless of however logical you believe your argument to be. Do not attack anyone personally for whatever reason - doing so contravenes the spirit in which this forum was founded and can have serious consequences.
Ta muchly,
Mac
It's unlikely to happen while you're there overseeing things .. but what happens when you go on holiday and someone else is responsible for the site? Then it's bound to happen .. that's how life works. It's pretty trivial to code defensively in a situation like this .. and there's no reason not to. Even if it's a million-to-one chance of the problem happening you might as well code against it just in case.SpiderMonkey wrote:I am well aware of the issue you mentioned, I simply didn't bother with it because it is so unlikely to occur with sensibly named fileds. Given that users aren't going to be writing files to your web server without you being able to filter out idiotic filenames, I didn't consider it was an issue.
-
SpiderMonkey
- Forum Commoner
- Posts: 85
- Joined: Fri May 05, 2006 4:48 am
Maybe thats so, but he didnt have to talk down to me like I'm a moron just because I don't believe its nessecary in all situations (like I said, the code in question is currently running just fine on a live site).onion2k wrote:It's unlikely to happen while you're there overseeing things .. but what happens when you go on holiday and someone else is responsible for the site? Then it's bound to happen .. that's how life works. It's pretty trivial to code defensively in a situation like this .. and there's no reason not to. Even if it's a million-to-one chance of the problem happening you might as well code against it just in case.SpiderMonkey wrote:I am well aware of the issue you mentioned, I simply didn't bother with it because it is so unlikely to occur with sensibly named fileds. Given that users aren't going to be writing files to your web server without you being able to filter out idiotic filenames, I didn't consider it was an issue.
Also, when handing over the website over to someone else I'd consider readability of the code to be more important than accounting for a mistake they almost certainly won't make. My code probably wouldn't require a comment, the more complicated version would I think.