Ravi Sharma
Ravi Sharma

Reputation: 31

How to get file name and its full path with PHP using scandir

I want to print file name and its full path of files in a folder and sub-folders also. My Code:

function listFolderFiles($dir){
    $ffs = scandir($dir);
    echo '<ol>';
    foreach($ffs as $ff){
        if($ff != '.' && $ff != '..'){
            echo '<li>'.$ff;
            if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
            echo '</li>';
        }
    }
    echo '</ol>';
}

listFolderFiles('Main Dir');

But it is only printing the file name not the path.

Upvotes: 2

Views: 9653

Answers (2)

Sanjay Kumar N S
Sanjay Kumar N S

Reputation: 4739

Try this:

function listFolderFiles($dir){
    $ffs = scandir($dir);
    echo '<ol>';
    foreach($ffs as $ff){
        if($ff != '.' && $ff != '..'){
            echo '<li>'.$ff;
            echo "   Real Path: ". $dir.'/'.$ff;
            if(is_dir($dir.'/'.$ff)) 
                    listFolderFiles($dir.'/'.$ff);
            echo '</li>';
        }
    }
    echo '</ol>';
}

listFolderFiles('/var/www/TestFiles');

Upvotes: 7

Afsar
Afsar

Reputation: 3124

check out this:

  echo '<li>'.$ff ." and full path is ". realpath($ff) . PHP_EOL;

Upvotes: 0

Related Questions