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?" />
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
.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With