Dan
Dan

Reputation: 12096

filectime vs filemtime for file modification time?

I'm trying to ensure some images aren't cached when they're modified but which would be more suitable for this filectime or filemtime?

I can't really see much difference from the php manuals? Would either be faster?

<img src="/images/123.png?<?=md5(@filectime("/images/123.png"))?>" />
<img src="/images/123.png?<?=md5(@filemtime("/images/123.png"))?>" />

Also is there a function like this that doesn't emit an e_warning on file error?

Ideally I don't want to ever serve just the question mark <img src="/images/123.png?" />

Upvotes: 17

Views: 13479

Answers (4)

JamesG
JamesG

Reputation: 1697

As you're dealing with image caching, filectime is inappropriate - it marks the last time:

when the permissions, owner, group, or other metadata from the inode is updated source: php.net

You want to know if the image file content has changed - if it's been resized, cropped or replaced entirely.

Therefore, filemtime is more suitable for your application:

when the data blocks of a file were being written to, that is, the time when the content of the file was changed source: php.net

If you don't want the ? to always appear, set filemtime to a variable first and test for it:

$filemtime = @filemtime("/images/123.png");

<img src="/images/123.png<?= $filemtime ? '?' . $filemtime : ''?>" />

Better still, test for the existence of the file with file_exists() before using filemtime.

Upvotes: 20

HoldOffHunger
HoldOffHunger

Reputation: 20901

TLDR Answer

  • filemtime: When the file's data was changed.
  • filectime: When the file's metadata was changed (i.e., depends on your Linux system, it might just be permissions or it could be anything like last access or write time).

2023 Explanation with Sources

Let's actually look at what the official PHP documentation says, circa 2023:

filemtime:

This function returns the time when the data blocks of a file were being written to, that is, the time when the content of the file was changed.

filectime:

Gets the inode change time of a file.

What's an inode? According to Wikipedia, it is basically file metadata, or...

...a data structure in a Unix-style file system that describes a file-system object such as a file or a directory.

Can this be broken down a bit more? In fact, Inode data can be anything from stuff like last timestamp, first timestamp to permissions, and isn't creation date (i.e., first timestamp) what we actually want to begin with?

To quote BealDung.com on Linux file syste metadata...

The inode stores the metadata associated with a file, such as a file’s access permissions, last access timestamp, owner, group, size, and the location of the file’s data.

It all depends on the details! And in Lamp development, this particular question ends up depending on your Linux setup and how your file system handles metadata.

Upvotes: 0

Tirumala
Tirumala

Reputation: 49

filemtime: Represents when the data or content is changed or modified, not including that of meta data such as ownership or ownergroup.

filectime: Represents the time when the meta data or inode data of a file is altered, such as the change of permissions, ownership or group.

so filemtime is the solution.

Upvotes: 2

Ulrich Thomas Gabor
Ulrich Thomas Gabor

Reputation: 6654

Note: In most Unix filesystems, a file is considered changed when its inode data is changed; that is, when the permissions, owner, group, or other metadata from the inode is updated. See also filemtime() (which is what you want to use when you want to create "Last Modified" footers on web pages) and fileatime().

From the manual

So you want to use filemtime.

Upvotes: 1

Related Questions