如何在Mac上获得PHP文件的实际创建时间? [英] How do I get actual creation time for a file in PHP on a Mac?

查看:297
本文介绍了如何在Mac上获得PHP文件的实际创建时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你在Finder中选择一个文件并在Mac上点击cmd + i时,你会得到文件被创建的时间以及上次修改的时间。



我的问题很简单:如何从PHP已经存在的Mac文件创建实际时间?



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



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



另外,我们可能只是在这里谈论Mac,但也欢迎与操作系统无关的解决方案 - 如果它们真的可以在所有系统上运行的话。

解决方案这个脚本是我管理的最好的脚本,它封装了BSD提供的命令行 stat 工具,以提供inode birthtime属性。

  // stat.php 
$ filename ='test';

$ stat = stat($ filename);
date_default_timezone_set('America / Denver');
echo strftime(atime:%H:%M:%S\\\
,$ stat ['atime']);
echo strftime(mtime:%H:%M:%S\\\
,$ stat ['mtime']);
echo strftime(ctime:%H:%M:%S\\\
,$ stat ['ctime']);

if($ handle = popen('stat -f%B'。escapeshellarg($ filename),'r')){
$ btime = trim(fread($ handle,100 ));
echo strftime(btime:%H:%M:%S\\\
,$ btime);
pclose($ handle);

命令行 stat 工具读取atime,ctime,mtime与PHP的统计完全一致,但是出现了第四个inode出生时间参数。 BSD stat()系统调用在可用时会返回st_birthtime,但是我还没有找到一种方法将本地暴露给PHP。

  $ touch test#创建文件
$ stat test
...May 30 06:16:22 2011May 30 06: 16:22 20115月30日06:16:22 20115月30日06:16:11 2011...
$ open。
$ touch test#大约一分钟后
$ stat test
...May 30 06:17:04 2011May 30 06:17:04 2011May 30 06 :17:04 2011May 30 06:16:11 2011...

$ php stat.php
atime:06:52:48
mtime:06 :17:04
ctime:06:17:04
btime:06:16:11

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

测试
1306757771


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.

解决方案

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

这篇关于如何在Mac上获得PHP文件的实际创建时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆