1

Possible Duplicate:
File creation time

I want to get all the files in a directory along with its date of creation in array.

I saw the scandir function, but it returns even the directory which I don't want. I also figured out that filemtime($filename) function can return the file created date. Just not able to put it together.

Community
  • 1
  • 1
esafwan
  • 17,311
  • 33
  • 107
  • 166

3 Answers3

14

in these days of PHP > 5 you should be using the provided classes for this kind of thing. For this particular task you should be using the FilesystemIterator for example:-

$iterator = new FilesystemIterator('D:/dev/');
foreach($iterator as $fileInfo){
    if($fileInfo->isFile()){
        $cTime = new DateTime();
        $cTime->setTimestamp($fileInfo->getCTime());
        echo $fileInfo->getFileName() . " file Created " . $cTime->format('Y-m-d h:i:s') .  "<br/>\n";
    }
    if($fileInfo->isDir()){
        $cTime = new DateTime();
        $cTime->setTimestamp($fileInfo->getMTime());
        echo $fileInfo->getFileName() . " dir Modified " . $cTime->format('Y-m-d h:i:s') . "<br/>\n";
    }
}

Here $fileInfo is an instance of SplFileInfo which can give you all the information about a file or directory you could ever want.

vascowhite
  • 18,120
  • 9
  • 61
  • 77
3
if ($handle = opendir('/path/to/files')) {

    while (false !== ($file = readdir($handle))) { 
        echo filemtime($file)."&nbsp;&nbsp;".$file;
     }

    closedir($handle); 
}
Sibu
  • 4,609
  • 2
  • 26
  • 38
1

Use scandir to get all the files and sub-directory of main directory

then use is_file function of php.

If you want to access specific extensions file please use glob function of php

Here is the example :

How to get only images using scandir in PHP?

I hope this helps.

Community
  • 1
  • 1
Dhruv Patel
  • 404
  • 1
  • 5
  • 16