1

i want to list out all files in a directory wallpaper/. there are more than 1000 folders inside this directory which are mainly celebrity name like angelina joulie etc and inside that folder several images are there for that celebrity.

i want to list all files with total path like http://xyz.com/wallpaper/angelina joulie/angeli1243.jpg

i tried scandir() readdir() glob() but all lists only current directory.

please tell how to list all files full path including subdirectory of a given directory

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
raviloves
  • 197
  • 1
  • 3
  • 12

2 Answers2

0

You need a recursive function. Here is a sample function that's tested and working properly.

function recursiveReaddir($folderPath = '.')
{
    $ignore = array('cgi-bin', '.', '..');
    $handle = @opendir($folderPath);
    while (false !== ($file = readdir($handle))) {

        if (!in_array($file, $ignore)) {
            if (is_dir("$folderPath/$file")) {
                $return[$file] = recursiveReaddir("$folderPath/$file");
            } else {
                $return [] = $file;
            }
        }
    }

    closedir($handle);
    return $return;
}
$rez = recursiveReaddir('path/to/file');
print_r($rez);
Vlad Preda
  • 9,780
  • 7
  • 36
  • 63
0

If you only need to go two levels in, it would be far easier to use glob():

glob("wallpaper/*/*")

Will traverse all immediate directories underneath wallpaper/.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • i am trying to upload data to mysql table but whereever any gap is not there and all lines are in new line it does the work but whereever lines are like this www.xyz.com/xyzk/dhdjje ejjj.jpg it breaks and stores words into databse any new tutorial can u please suggest to import data – raviloves Feb 02 '13 at 15:46