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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s