Page 1 of 1

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

Posted: Mon Mar 07, 2011 1:56 am
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.

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

Posted: Mon Mar 07, 2011 1:58 am
by bappi-d-great
In here...al the sub folders (service, client, contact) are under content folder....