Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Warning: filemtime(): stat failed for a file that should exist

ErrorWarning: filemtime(): stat failed for

Short 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.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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));
  2. 2Match what you see, then jump to its fix

    1. 1file_exists() returns false for the same stringThe path does not point to a real fileFix
    2. 2The file exists on disk but PHP still cannot reach it; the path is outside the allowed treeopen_basedir blocks the locationFix
    3. 3The argument starts with http:// or https://A URL was passed to filemtime()Fix

What does the error look like?

  • Warning: filemtime(): stat failed for
  • filemtime(): 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

Did this fix it?

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:/tmp

3Verify

Request the page again and check the warning does not return.

sudo systemctl reload php8.3-fpm

Confirmed by php.net

Did this fix it?

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.css

Confirmed by php.net

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.