Tuesday 14 August 2018

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));
        }else{
            if(strstr($dir,".pdf")){
                $directories []= $dir;
            }
        }
    }
    return $directories;
}

$directories = expandDirectories($directory);

print_r($directories);

You will get list of pdf file with directory name.

0 comments:

Post a Comment