How do I get actual creation time for a file in PHP on a Mac?

对着背影说爱祢 提交于 2019-12-09 17:12:20

问题


When you choose a file in Finder and hit cmd+i on a Mac, you get the time the file was (actually) created, and the time it was last modified.

My question is simply: How can I get the actual time of creation from an already existing Mac file with PHP?

Now, having researched the topic, I have read posts that say it is impossible, but in my world "impossible" only means that a thing takes a bit longer to accomplish. Workarounds and hacks are welcomed.

I do not want mtime or ctime related advice, as these only access the last time the file was updated or modified.

Also we're probably talking Mac only here, but OS independent solutions are also welcome - if they really work on all systems.


回答1:


This script is the best I've managed, which wraps the command-line stat tool available on BSD to come up with the inode birthtime 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 command-line stat tool reads atime, ctime, mtime exactly like PHP's stat, but comes up with a fourth "inode birth time" parameter. The BSD stat() system call returns st_birthtime when available, but I haven't found a way to natively expose this to 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 a unix timestamp of only the inode birthtime, which is the best I've found so far. You can run it with popen() or proc_open()

$ stat -f %B test
1306757771



回答2:


MacOS X has an extended version of the stat() system call that also returns the file creation time, but it's not enabled by default (even in native C code) as the resulting structure has its fields in a different order to those in the standard POSIX version.

In 10.6 that version is provided by the (hidden) symbol _stat$INODE64 in /usr/lib/libc.dylib which is automatically substituted for stat if the macro _DARWIN_FEATURE_64_BIT_INODE is defined.

If you can figure out how to access that symbol from the dynamic library, job done!




回答3:


The only closest you can get is the last updated time by the filemtime function.



来源:https://stackoverflow.com/questions/6176140/how-do-i-get-actual-creation-time-for-a-file-in-php-on-a-mac

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!