PHP:如何获取文件创建日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4401320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:56:44  来源:igfitidea点击:

PHP: how can I get file creation date?

phpfile

提问by zod

I am reading a folder with lots of files.

我正在阅读一个包含大量文件的文件夹。

How can I get the creation date of a file. I don't see any direct function to get it.

如何获取文件的创建日期。我没有看到任何直接的功能来获得它。

There are filemtimeand filectime.

filemtimefilectime

And if the file hasn't been modified, what will happen?

如果文件没有被修改,会发生什么?

回答by Alin Purcaru

Use filectime. For Windows it will return the creationtime, and for Unix the changetime which is the best you can get because on Unix there is no creation time (in most filesystems).

使用filectime。对于 Windows,它将返回创建时间,而对于 Unix,它将返回更改时间,这是您可以获得的最佳时间,因为在 Unix 上没有创建时间(在大多数文件系统中)。

Note also that in some Unix texts the ctime of a file is referred to as being the creation time of the file. This is wrong. There is no creation time for Unix files in most Unix filesystems.

另请注意,在某些 Unix 文本中,文件的 ctime 被称为文件的创建时间。这是错误的。大多数 Unix 文件系统中都没有 Unix 文件的创建时间。

回答by Rakesh Dongarwar

$filename = 'somefile.txt';

if (file_exists($filename)) {

    echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}

回答by mrwooster

Unfortunately if you are running on linux you cannot access the information as only the last modified date is stored.

不幸的是,如果您在 linux 上运行,则无法访问该信息,因为仅存储了最后修改的日期。

It does slightly depend on your filesystem tho. I know that ext2 and ext3 do not support creation time but I think that ext4 does.

它确实略微取决于您的文件系统。我知道 ext2 和 ext3 不支持创建时间,但我认为 ext4 支持。

回答by SDukla

I know this topic is super old, but, in case if someone's looking for an answer, as me, I'm posting my solution.

我知道这个话题非常古老,但是,如果有人像我一样在寻找答案,我会发布我的解决方案。

This solution works IF you don't mind having some extra data at the beginning of your file.

如果您不介意在文件开头添加一些额外数据,则此解决方案有效。

Basically, the idea is to, if file is not existing, to create it and append current date at the first line. Next, you can read the first line with fgets(fopen($file, 'r')), turn it into a DateTimeobject or anything (you can obviously use it raw, unless you saved it in a weird format) and voila - you have your creation date! For example my script to refresh my log file every 30 days looks like this:

基本上,这个想法是,如果文件不存在,则创建它并在第一行附加当前日期。接下来,您可以使用 阅读第一行fgets(fopen($file, 'r')),将其转换为DateTime对象或任何东西(您显然可以使用原始数据,除非您以奇怪的格式保存它),瞧——您有了创建日期!例如,我每 30 天刷新一次日志文件的脚本如下所示:

if (file_exists($logfile)) {
            $now = new DateTime();
            $date_created = fgets(fopen($logfile, 'r'));
            if ($date_created == '') {
                file_put_contents($logfile, date('Y-m-d H:i:s').PHP_EOL, FILE_APPEND | LOCK_EX);
            }
            $date_created = new DateTime($date_created);
            $expiry = $date_created->modify('+ 30 days');
            if ($now >= $expiry) {
                unlink($logfile);
            }
        }