How get all the PDF files from directory and sub directory

For check pdf files in directory and sub directory

First we have to read folder and we will get directory and file name from folder.

then we will check is directory or not.

if we get directory then we again call the same function for get sub directory and file from current directory

and we will separate pdf file name from file extension.

Following code will help you for get pdf file list from mentioned directory name or we can also get all files from directory

$directory = 'typo3/fileadmin';

function expandDirectories($base_dir) {

    $directories = array();

    foreach(scandir($base_dir) as $file) {
        if($file == '.' || $file == '..') continue;

        $dir = $base_dir.DIRECTORY_SEPARATOR.$file;

        if(is_dir($dir)) {
            $directories = array_merge($directories, expandDirectories($dir));
                $directories []= $dir;
    return $directories;

$directories = expandDirectories($directory);


You will get list of pdf file with directory name.

i hope it will work for you šŸ™‚ njoy šŸ˜‰

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya