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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
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.

Post 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.
SpiderMonkey
Forum Commoner
Posts: 85
Joined: Fri May 05, 2006 4:48 am

Post 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.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

php 4.0 +
SpiderMonkey
Forum Commoner
Posts: 85
Joined: Fri May 05, 2006 4:48 am

Post 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.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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);

?>
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

oooooooh thanks Oren

thanks much
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post 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.
SpiderMonkey
Forum Commoner
Posts: 85
Joined: Fri May 05, 2006 4:48 am

Post 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...
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There's a posted set of snippets in Code Snippets that perform tree fetching....
SpiderMonkey
Forum Commoner
Posts: 85
Joined: Fri May 05, 2006 4:48 am

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
SpiderMonkey
Forum Commoner
Posts: 85
Joined: Fri May 05, 2006 4:48 am

Post 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.
Post Reply