file_get_contents with relative path

file_get_contents with relative path

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why file_get_contents() Fails with Relative Paths in PHP Scripts

When working with file system paths in PHP, developers often assume that a relative path is resolved against the current script's location. However, file_get_contents() behaves differently depending on whether you are executing a command from the CLI or embedding code within a web server context. This distinction explains why attempts to load /var/www/base/validate/edit.json directly using simple relative strings like '../validate/edit.json' frequently result in "No such file or directory" errors, even when the file physically exists in that location.

The Default Resolution Anchor

In standard PHP execution contexts, particularly when a script is accessed via HTTP, file_get_contents() resolves relative paths against the current working directory of the process, not the directory containing the executing script. By default, the current working directory is set to /var/www/html (or the document root), regardless of which subdirectory contains your active .php file.

If you attempt:

$json = file_get_contents('../validate/edit.json');

while running detail.php located at /var/www/base/controller/detail.php, PHP interprets this as looking for the file inside /var/www/html/../validate/edit.json. This path resolves to /var/www/validate/edit.json, which does not exist. The file you need resides in /var/www/base/, requiring a traversal up two levels from the document root, not one.

Avoiding FILE_USE_INCLUDE_PATH

A common misconception is that the FILE_USE_INCLUDE_PATH flag changes this behavior for file_get_contents(). This flag affects how autoloader paths are resolved in specific contexts or when using include_path, but it does not alter the base path resolution for file_get_contents() calls. As demonstrated in your testing attempts, adding this flag to './validate/edit.json' or '../validate/edit.json' yields no change in outcome; the error persists because the underlying resolution anchor remains the process's current working directory, not the script's physical location.

The Working Solution: Script-Based Pathing

To reliably access files relative to the script itself, you must explicitly construct the path using functions that return the script's actual file system location. The combination of dirname() and __FILE__ provides a robust solution that is independent of the server's current working directory.

The pattern dirname(dirname(__FILE__)) . '/validate/edit.json' works because:

  1. __FILE__ contains the full absolute path to detail.php.
  2. The first dirname() call strips the filename, leaving /var/www/base/controller.
  3. The second dirname() call moves up one level, resulting in /var/www/base.
  4. Concatenating the target directory ensures the final path is correct: /var/www/base/validate/edit.json.

Modern Alternatives and Best Practices

While constructing paths manually works, it introduces fragility if your directory structure changes. A more maintainable approach for modern PHP applications is to use pathinfo() or the SplFileInfo class, which allows you to manipulate the script's path dynamically without hardcoding directory levels. For example:

$scriptPath = __DIR__ . '/validate/edit.json'; // Works if validate/ is in same dir as detail.php
// Or for nested structures:
$baseDir = dirname(__DIR__); 
$json = $baseDir . '/validate/edit.json';

Using __DIR__ (available since PHP 5.3) is often cleaner than chaining multiple dirname() calls, provided the target directory structure aligns with the script's location. If your application relies heavily on relative file loading across complex directory trees, consider defining a constant base path at the top of your configuration files rather than calculating it repeatedly within logic blocks.

Conclusion

The failure of simple relative paths in file_get_contents() is a direct result of PHP resolving them against the server's current working directory rather than the script's location. By explicitly anchoring your file paths to __FILE__ or __DIR__, you ensure that your application remains functional regardless of how the web server or CLI sets its working directory.

Our network