Page 1 of 2

i want to list down files and folders of a directory.

Posted: Tue Jun 06, 2006 6:36 am
by itsmani1
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.

Posted: Tue Jun 06, 2006 6:42 am
by SpiderMonkey
Which version of php is your server running? Might seem a silly question but I recently did something similar and was tripped up by the older version of php I was forced to work with.

Posted: Tue Jun 06, 2006 6:51 am
by itsmani1
php 4.0 +

Posted: Tue Jun 06, 2006 7:03 am
by SpiderMonkey
OK, here is how I did it:

Code: Select all

$dir=opendir("directory");

while ($file=readdir($dir)) {
  ...
}
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

Code: Select all

is_dir($file)
to pick out the subdirectories and call your function recursively.

Posted: Tue Jun 06, 2006 7:35 am
by Oren
This is the wrong way to do it.
You should do it like this:

Code: Select all

<?php

$dir = "/tmp";
$dh  = opendir($dir);

while (false !== ($filename = readdir($dh)))
{
   $files[] = $filename;
}

?>
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:

Code: Select all

<?php

$dir    = '/tmp';
$files = scandir($dir);

?>

Posted: Tue Jun 06, 2006 7:57 am
by itsmani1
oooooooh thanks Oren

thanks much

Posted: Tue Jun 06, 2006 8:01 am
by itsmani1
there is a small issue, its not showing me folder, its showing me files with out any problem, but i want to see files list as well.

Please.

Posted: Tue Jun 06, 2006 8:11 am
by SpiderMonkey
Oren wrote:This is the wrong way to do it.
No it isn't.
In SpiderMonkey's way, a file named '0' for instance, will end the loop.
Its best to avoid giving files such stupid names anyway, even if you aren't using this function.
FYI: In PHP 5 you could do exactly the same thing with this code:

Code: Select all

<?php

$dir    = '/tmp';
$files = scandir($dir);

?>
Which is why I asked which version he was using first...

Posted: Tue Jun 06, 2006 8:28 am
by Oren
SpiderMonkey wrote:No it isn't.
Yes it is.
SpiderMonkey wrote:Its best to avoid giving files such stupid names anyway, even if you aren't using this function.
That's your argument?

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.

Posted: Tue Jun 06, 2006 8:44 am
by itsmani1
i did not get what you mean by '0'? can you describe it, 2ndly i can't see folder, i can see list of files, how can i view my folders?

thanks much.

Posted: Tue Jun 06, 2006 8:46 am
by feyd
There's a posted set of snippets in Code Snippets that perform tree fetching....

Posted: Tue Jun 06, 2006 9:06 am
by SpiderMonkey
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.
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.

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.

Posted: Tue Jun 06, 2006 9:14 am
by twigletmac
Just a friendly note to SpiderMonkey and Oren:
[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.
Please keep it civil.

Ta muchly,
Mac

Posted: Tue Jun 06, 2006 9:14 am
by onion2k
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.
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.

Posted: Tue Jun 06, 2006 9:45 am
by SpiderMonkey
onion2k wrote:
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.
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.
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).

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.