Warning: filemtime(): stat failed for a file that should exist
Warning: filemtime(): stat failed forShort answer
filemtime() returns false and raises a warning when PHP cannot stat the path you gave it. The path may point to nothing, PHP may be blocked from reaching it by open_basedir, or you passed a URL whose wrapper cannot stat. Print the path, build it from __DIR__, and check each case in turn.
Fix it fast
1Find out which cause you have
Print the exact string passed to filemtime() and ask PHP about it. If file_exists() says false, the path is the problem; if it says true, look at the other causes.
<?php $path = 'assets/style.css'; var_dump($path, getcwd(), file_exists($path));2Match what you see, then jump to its fix
What does the error look like?
Warning: filemtime(): stat failed forfilemtime(): stat failed for /var/www/html/assets/style.css
Cause 1 of 3
The path does not point to a real file
1Check
A typo, a wrong folder, a file not deployed yet, or a path that works from one script but not from another. The manual documents __DIR__ as the directory of the current file, which gives a path that does not depend on where the request started.
<?php
var_dump(__DIR__, file_exists(__DIR__ . '/assets/style.css'));2Fix
Build the path from __DIR__ (or from a configured absolute base path) and guard the call, since filemtime() returns false on failure.
<?php
$file = __DIR__ . '/assets/style.css';
$version = file_exists($file) ? filemtime($file) : time();3Verify
Load the page and check the warning is gone and the version number is a Unix timestamp.
php -r 'echo filemtime(__FILE__), PHP_EOL;'Confirmed by php.net
Cause 2 of 3
open_basedir blocks the location
1Check
open_basedir limits PHP to the listed directory trees and refuses files outside them. Read the value PHP is really using.
php -r 'var_dump(ini_get("open_basedir"));'2Fix
Move the file inside an allowed directory, or add its directory to open_basedir in the PHP configuration (separate paths with a colon on Linux, a semicolon on Windows). Reload PHP-FPM or Apache afterwards.
; php.ini or the pool file
open_basedir = /var/www/html:/var/www/shared:/tmp3Verify
Request the page again and check the warning does not return.
sudo systemctl reload php8.3-fpmConfirmed by php.net
Cause 3 of 3
A URL was passed to filemtime()
1Check
The manual says filemtime() works only with some URL wrappers: the ones that support the stat() family. A plain HTTP address is not a local file.
<?php
var_dump(parse_url($path, PHP_URL_SCHEME));2Fix
Turn the URL into the local file path on the server and call filemtime() on that.
<?php
$local = $_SERVER['DOCUMENT_ROOT'] . '/assets/style.css';
$version = filemtime($local);3Verify
Print the value and compare it with the file's time on disk.
stat -c %Y /var/www/html/assets/style.cssConfirmed by php.net
How do I stop it coming back?
- Never build file paths from relative strings; start from __DIR__ or one configured base directory.
- Wrap filemtime() in a small helper that checks the file first and returns a fallback.
- Check open_basedir when moving a site to a new host or pool.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix