Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. if(filemtime('myfile.txt') > $result_set['filemtime']) {
  2. // file was modified
  3. }
  4.  
  5. $modifiedTs = filemtime($filename);
  6. if ($modifiedTs != $lastModificationTs){
  7. echo "$filename was modified!";
  8. }
  9.  
  10. class ApplicationLogger {
  11.  
  12. private $logfile, $path;
  13.  
  14. public function __construct() {
  15. $this->logfile = 'error.log';
  16. $this->path = '/var/log/apache2/';
  17. }
  18.  
  19. public function logState() {
  20. $files = array(
  21. 'target' => $this->path . $this->logfile,
  22. 'lastmod' => $this->path . $this->logfile . '_lastmod'
  23. );
  24.  
  25. if (file_exists($files['lastmod']) && file_exists($files['target'])) {
  26. $lfh = fopen($files['lastmod'], 'r');
  27. while (!feof($lfh)) {
  28. $lines[] = fgets($lfh);
  29. }
  30. fclose($lfh);
  31. }
  32.  
  33. $modified = false;
  34.  
  35. /**
  36. * check if we have a matching hash.
  37. */
  38. if (isset($lines) && filemtime($files['target']) != $lines[0]) { // mod time mismatch
  39. if (md5_file($files['target']) != $lines[1]) { // content modified
  40. $modified = true;
  41. }
  42. }
  43.  
  44. /**
  45. * update or create the lastmod file
  46. */
  47. if (!file_exists($files['lastmod']) || $modified) {
  48.  
  49. $current_mod = filemtime($files['target']) . "n" . md5_file($files['target']);
  50.  
  51. file_put_contents($files['lastmod'], $current_mod);
  52. $modified = true;
  53. }
  54.  
  55. return $modified;
  56. }
  57. }
  58.  
  59. $mod = new ApplicationLogger();
  60. if ($mod->logState()) {
  61. // changed do something
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement