1

a couple of days ago (unfortunately) my web site has been hacked. In order to prevent it I'd like to include a PHP which checks if any file has been changed/added in the last 2 days. My starting point is the following function:

function topmods($dir)
{
  $mods = array();
  foreach (glob($dir . '/*') as $f) {
    $mods[] = filemtime($f);
  }
  sort($mods);
  $mods = array_reverse($mods);
  return $mods;
}

I'd like hower to improve it so that only files changed in the last 2 days are included in the array. Any suggestion how to rewrite the foreach function so that you include in the array only files that satisfy this condition?

if (time() - filemtime($testfile) >= 2 * 86400)

Thanks

Carla
  • 3,064
  • 8
  • 36
  • 65
  • You can verify your code : http://stackoverflow.com/questions/3126191/php-script-to-delete-files-older-than-24-hrs-deletes-all-files , Instead of delete you can make array of files – Niklesh Raut Mar 19 '16 at 09:07

1 Answers1

0

As filemtime() get the unix timestamp, you can create a unix timestamp from 2 days ago and compare, like this:

function topmods($dir)
{
    // Get actual timestamp and subtract 2 days
    $dateToCompare = time()-((60 * 60 * 24)*2); 
    $mods = [];
    foreach (glob($dir . '/*') as $f) {
        // This just compares the two timestamp values
        if (filemtime($f)<$dateToCompare) {
            $mods[] = filemtime($f);
        }
    }
    sort($mods);
  $mods = array_reverse($mods);
  return $mods;
}
William Prigol Lopes
  • 1,803
  • 14
  • 31