Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting last modification date of files in directory using PHP

Tags:

date

php

I am trying to get the last modification date of all files in a directory using PHP.

I am using this:

foreach($dir as $file) 
{
$mod_date=date("F d Y H:i:s.", filemtime($file));
}

foreach($dir as $file) is returning the correct files, but all of the modification dates are coming back as 0000-00-00 00:00:00, instead of the actual modification date.

What changes do I need to make to get this working?

like image 283
Nick Avatar asked Aug 07 '12 20:08

Nick


1 Answers

Check if the $file var is actually pointing to a correct file

foreach($dir as $file) 
{
  if(is_file($file))
  {
    $mod_date=date("F d Y H:i:s.", filemtime($file));
    echo "<br>$file last modified on ". $mod_date;
  }
  else
  {
    echo "<br>$file is not a correct file";
  }
}
like image 167
raidenace Avatar answered Oct 21 '22 05:10

raidenace