【问题标题】:How do I get actual creation time for a file in PHP on a Mac?如何在 Mac 上获取 PHP 文件的实际创建时间?
【发布时间】:2011-05-30 12:12:38
【问题描述】:

当您在 Finder 中选择一个文件并在 Mac 上按 cmd+i 时,您会获得该文件(实际)创建的时间,以及它最后一次修改的时间。

我的问题很简单:如何使用 PHP 从现有的 Mac 文件中获取实际创建时间?

现在,在研究了这个话题之后,我读过一些帖子说这是不可能的,但在我的世界里,“不可能”只是意味着一件事需要更长的时间才能完成。欢迎使用解决方法和技巧。

我不想要与 mtime 或 ctime 相关的建议,因为它们只访问文件上次更新或修改的时间。

此外,我们在这里可能只讨论 Mac,但也欢迎独立于操作系统的解决方案 - 如果它们真的适用于所有系统。

【问题讨论】:

  • POSIX 没有指定文件创建时间属性
  • BSD 支持 (st_birthtime),其中包括 OSX! :)

标签: php macos file time


【解决方案1】:

这个脚本是我管理过的最好的脚本,它包装了 BSD 上可用的命令行 stat 工具来提供 inode 出生时间属性。

// 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);
}

命令行stat 工具读取atime、ctime、mtime 与PHP 的stat 完全一样,但带有第四个“inode 诞生时间”参数。 BSD stat() 系统调用在可用时返回 st_birthtime,但我还没有找到一种方法将其本地公开给 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

以下命令返回 inode 出生时间的 unix 时间戳,这是迄今为止我发现的最好的。您可以使用popen()proc_open() 运行它

$ stat -f %B test
1306757771

【讨论】:

  • 酷!这看起来很有希望。谢谢!
【解决方案2】:

MacOS X 具有 stat() 系统调用的扩展版本,它也返回文件创建时间,但默认情况下不启用(即使在本机 C 代码中),因为生成的结构的字段顺序与那些顺序不同在标准 POSIX 版本中。

在 10.6 中,该版本由 /usr/lib/libc.dylib 中的(隐藏)符号 _stat$INODE64 提供,如果定义了宏 _DARWIN_FEATURE_64_BIT_INODE,则会自动替换 stat

如果您能弄清楚如何从动态库中访问该符号,那就大功告成了!

【讨论】:

  • 酷!不过,访问符号似乎很困难......这只是取消注释一行的问题,还是更多?另外,我想知道,它在 Mac OS X 10.5 中有什么不同吗?
【解决方案3】:

您可以得到的唯一最接近的是 filemtime 函数的最后更新时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    相关资源
    最近更新 更多