0

very common question and still did not find the right solution for this. I need to run cron job to start php script each morning to list all new files created on the web server during the night. This is very usefull to see what visitors have uploaded during the night and ofcourse very ofter those files could be harmful files that will hurt other visitors computers. So far I have this:

$dir = "../root/";         
$pattern = '\.*$'; // check only file with these ext.              
$newstamp = 0;                
$newname = "";    
if ($handle = opendir($dir)) {                   
       while (false !== ($fname = readdir($handle)))  {                
         // Eliminate current directory, parent directory                
         if (ereg('^\.{1,2}$',$fname)) continue;                
         // Eliminate other pages not in pattern                
         if (! ereg($pattern,$fname)) continue;                
         $timedat = filemtime("$dir/$fname");                
         if ($timedat > $newstamp) {    
            $newstamp = $timedat;    
            $newname = $fname;    
          }    
         }    
        }    
closedir ($handle);    

// $newstamp is the time for the latest file    
// $newname is the name of the latest file    
// print last mod.file - format date as you like                
print $newname . " - " . date( "Y/m/d", $newstamp);    

this prints the newest file but only in one directory root/ and doesnt check for example root/folder/ and etc.. How to do it recusrsivly ? If I add a new file within root/folder the script will show me folder with date , but would not show which file in root/folder was created.. I hope you understand what I mean, thanks

thecore7
  • 482
  • 2
  • 7
  • 27

1 Answers1

3

Quick script that does what you want (tested under windows 7 with cygwin and under ubuntu 12.10 with PHP 5.3.10)

<?php
$path = $argv[1];
$since = strtotime('-10 second'); // use this for previous day: '-1 day'

$ite = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
foreach ( new RecursiveIteratorIterator($ite) as $filename => $object ) {
    if (filemtime($filename) > $since) {
      echo "$filename recently created\n";
    }
}

My quick test:

$> mkdir -p d1/d2
$> touch d1/d2/foo
$> php test.php .
./d1/d2/foo recently created
$> php test.php . # 10secs later 
$>
Benjamin Crouzier
  • 34,915
  • 36
  • 154
  • 219
  • chears pinouchon, but I am getting this error Fatal error: Class 'FilesystemIterator' not found in I am using php 4 – thecore7 Jan 29 '13 at 10:26
  • +1, although why use `filemtime()` when you're using the SPL classes? You could use `$filename->getMTime()`. I'd also rather see a `DateTime` object instead of `strtotime()`. But +1 anyway, because it's pretty much what I would have done. – SDC Jan 29 '13 at 10:27
  • @Мариян Маринов - `FilesystemIterator` is built into PHP 5.3 and up See http://php.net/manual/en/class.filesystemiterator.php. If you're on an older version, you should upgrade, because anything older than 5.3 is unsupported. But if you can't upgrade, this answer can still be used without the `FilesystemIterator` bit. – SDC Jan 29 '13 at 10:30
  • @МариянМаринов print $filename before the `if (filemtime($filename) > $since)` and see what happens. – Benjamin Crouzier Jan 29 '13 at 10:39
  • Sorry pinouchon, you are Right, I dint change -1 second and that's why didnt see all files, but is it possible to show the time of creation of the file too? – thecore7 Jan 29 '13 at 10:39
  • Check http://stackoverflow.com/questions/4401320/php-how-can-i-get-file-create-date and http://stackoverflow.com/questions/2084986/file-creation-time for getting file creation time – Benjamin Crouzier Jan 29 '13 at 10:41
  • I consider this is answered :)) very good man ! you safed my day :) – thecore7 Jan 29 '13 at 10:46