0

I wrote a CMS script made of many folders and files and I want to find a way to track when I last modified any of the files. I wrote a recursive directory/file check that finds the latest modified file and gives me the date and time however my issue is this: every time that I as much as copy a file to the server, or rename a file, even if I didn't make any modifications at all to any of the files, the newly copied file or renamed file now has today's date and therefore my script shows that there was a modification made today even if I haven't made changes in weeks.

How can I circumvent that? I am using filemtime()

Is there a way with PHP to know when the file was ACTUALLY last modified (ie when the code in a file was worked on the last time)? Thanks

marcnyc
  • 585
  • 1
  • 4
  • 17
  • 2
    the problem is you dont agree with the OS's definition of modified, in such a case you have to record that information your self –  Jul 22 '14 at 02:25
  • You could use an md5 hash to actually know when your file is modified. – Chibueze Opata Jul 22 '14 at 02:29
  • @ChibuezeOpata how would I do that? what do you mean? – marcnyc Jul 22 '14 at 02:32
  • I mean that you could store the md5 hash of the file anytime you check the last modified date, and cross-check this hash with the last saved hash to know if the content actually changed. You may consider changing the title of your question properly to get better answers. – Chibueze Opata Jul 22 '14 at 02:40
  • @ChibuezeOpata if I did what you suggest, I would have to store all the md5_file hashes of all the files, wouldn't I? or am I missing something? – marcnyc Jul 22 '14 at 03:25

1 Answers1

1

I found a way to do it and wanted to post the answer:

$test = new SplFileInfo('path/to/file');
echo  $test->getMTime();
echo date('Y-m-d',$test->getMTime());

The SplFileInfo::getMTime will actually return the last time a file's contents were modified as opposed to the last modification date of the file

marcnyc
  • 585
  • 1
  • 4
  • 17
  • `SPLFileInfo::getMTime()` accesses the exact same data as `filemtime()`. There's no difference. –  Jul 22 '14 at 04:58
  • @duskwuff I just ran some tests comparing `getMTime` and `filemtime`, I think `getMTime` is more reliable as `filemtime` can have strange behaviours on other OS, but I cannot say for certain they may have different values. – Chibueze Opata Jul 22 '14 at 13:39
  • If you refer to the relevant PHP source code ([spl_directory.c](https://github.com/php/php-src/blob/master/ext/spl/spl_directory.c) vs. [filestat.c](https://github.com/php/php-src/blob/master/ext/standard/filestat.c)), both functions call `php_stat()` and return the `FS_MTIME` field. Again, there is no difference between the two methods. –  Jul 22 '14 at 15:22
  • @duskwuff I would argue there is a difference because I get different results! if you, say, rename a file on the server, the FTP browser and filemtime() will return the time when you renamed the file, even if you didn't change the contents, while SPLFileInfo::getMTime() will still return the time the file's contents were last changed. – marcnyc Jul 22 '14 at 15:36
  • I'm quite certain that you're mistaken. The underlying implementations of the two functions are identical. They cannot possibly return different results. –  Jul 22 '14 at 16:30
  • @duskwuff I am here to learn and if you can teach me that I am wrong I accept it, I'm not trying to argue with you ;-) all I can tell you is this: it is now 3pm in NYC. The last modification I made to my files was at 1.27pm. If I now (at 3pm) rename a file on the server with filemtime() I get a result of 3pm but with getMTime() I get the result of 1.27pm – marcnyc Jul 22 '14 at 19:01