How to get all filename within a folder or sub folder?

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

Post Reply
User avatar
bappi-d-great
Forum Newbie
Posts: 2
Joined: Mon Mar 07, 2011 1:36 am
Location: Dhaka, Bangladesh
Contact:

How to get all filename within a folder or sub folder?

Post by bappi-d-great »

Hello All,

I am very new in this forum. I know i m going to ask a silly question but this problem gave me a big headache. I want to write a function which will read all the file name within a floder and sub-folder, but not the folder name and will return as an array.

For example:

- CONTENT
home.php
- SERVICE
software.php
hardware.php
- CLIENT
overseas_client.php
local_client.php
- CONTACT
query.php
location.php


In the above example the capital lettered words are directory name. I want only the file names in content folder. The returned value will be Array('home.php', 'software.php', 'hardware.php', 'overseas_client.php', 'local_client.php', 'query.php', 'location.php')

So, i wrote a function:

function getDirContent($dir)
{
$temp = array('.', '..', 'Thumbs.db');
$fileName = array();
$readDir = opendir($dir);
while($file = readdir($readDir)) {
if(!in_array($file, $temp))
if(is_dir($dir.'/'.$file))
getDirContent($dir.'/'.$file);
else
{
//echo $file;
array_push($fileName, $file);
}
}
return $fileName;
}

$dir = 'content;
$test = getDirContent($dir);
print_r($test);

But it shows only home.php nothing more. But if i uncomment the commented line (under else statement) i got all the file name being printed. But i need an array. What is the wrong in this code? Please anyone help me.

Thanks.
User avatar
bappi-d-great
Forum Newbie
Posts: 2
Joined: Mon Mar 07, 2011 1:36 am
Location: Dhaka, Bangladesh
Contact:

Re: How to get all filename within a folder or sub folder?

Post by bappi-d-great »

In here...al the sub folders (service, client, contact) are under content folder....
Post Reply