How to get actual file creation time in PHP on Mac?

When you select a file in Finder and press cmd + i on Mac, you get the time when the file was created (actually), and the time of the last change.

My question is simple: how can I get the actual creation time from an existing Mac file with PHP?

Now, exploring the topic, I read messages that say that this is impossible, but in my world โ€œimpossibleโ€ means only that a thing takes a little longer. Workarounds and hacks are welcome.

I do not need advice related to mtime or ctime, as they have access only the last time the file was updated or modified.

In addition, we are probably only talking about the Mac here, but OS-independent solutions are also welcome - if they really work on all systems.

+7
file php time macos
May 30 '11 at 12:12
source share
3 answers

This script is the best I have succeeded in wrapping the stat command line tool available in BSD to come up with the inode birth attribute.

 // stat.php $filename = 'test'; $stat = stat($filename); date_default_timezone_set('America/Denver'); echo strftime("atime: %H:%M:%S\n", $stat['atime']); echo strftime("mtime: %H:%M:%S\n", $stat['mtime']); echo strftime("ctime: %H:%M:%S\n", $stat['ctime']); if ($handle = popen('stat -f %B ' . escapeshellarg($filename), 'r')) { $btime = trim(fread($handle, 100)); echo strftime("btime: %H:%M:%S\n", $btime); pclose($handle); } 

The stat command line tool reads atime, ctime, mtime in the same way as PHP stat, but the fourth parameter, "inode time of birth," appears. The BSD stat() system call returns st_birthtime when it is available, but I have not found a way to expose it in PHP.

 $ touch test # create a file $ stat test ..."May 30 06:16:22 2011" "May 30 06:16:22 2011" "May 30 06:16:22 2011" "May 30 06:16:11 2011"... $ open . $ touch test # about one minute later $ stat test ..."May 30 06:17:04 2011" "May 30 06:17:04 2011" "May 30 06:17:04 2011" "May 30 06:16:11 2011"... $ php stat.php atime: 06:52:48 mtime: 06:17:04 ctime: 06:17:04 btime: 06:16:11 

The following command returns the unix timestamp of only the inode timestamp, and this is the best I've found so far. You can run it with popen () or proc_open ()

 $ stat -f %B test 1306757771 
+7
May 30 '11 at 12:26
source share

MacOS X has an extended version of the stat() system call, which also returns the file creation time, but by default it is not included (even in the C source code), since the resulting structure has its fields in a different order for those in the standard POSIX version.

In version 10.6, this version is provided by the (hidden) _stat$INODE64 in /usr/lib/libc.dylib , which is automatically replaced with stat if the _DARWIN_FEATURE_64_BIT_INODE macro is _DARWIN_FEATURE_64_BIT_INODE .

If you can figure out how to access this symbol from a dynamic library, complete the task!

+2
May 30 '11 at 12:43
source share

The only thing you can get is the last updated time using the filemtime function.

0
May 30 '11 at 12:31
source share



All Articles