Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filectime vs filemtime for file modification time?

Tags:

php

caching

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?" />

like image 285
Dan Avatar asked Feb 26 '14 13:02

Dan


3 Answers

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

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

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.

like image 77
JamesG Avatar answered Nov 10 '22 13:11

JamesG


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.

like image 28
Ulrich Thomas Gabor Avatar answered Nov 10 '22 15:11

Ulrich Thomas Gabor


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.

like image 2
Tirumala Avatar answered Nov 10 '22 15:11

Tirumala