Server Document Root Path in PHP

Server Document Root Path in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Why $_SERVER["DOCUMENT_ROOT"] Breaks Your glob() Calls

You are attempting to construct an absolute path for a file operation using $_SERVER["DOCUMENT_ROOT"], but your code fails when you switch from a relative path like 'myFolder/*' to the constructed string. The root cause isn't a bug in PHP's glob() function; it is a fundamental misunderstanding of how variables and strings interact in that specific context.

In your working example, $files = glob('myFolder/*');, you are passing a literal string containing the characters m, y, F, o, l, d, e, r. The shell-like pattern matcher sees this as a directory named "myFolder" relative to the current execution context.

When you change it to $files = glob($_SERVER["DOCUMENT_ROOT"]."/myFolder/*");, you are inadvertently passing a variable reference instead of a string. PHP interprets the expression inside glob() as an expression that needs to be evaluated before being passed to the function. Because the variable is enclosed in quotes only when written as a literal string, omitting those outer quotes causes PHP to try to concatenate strings with a variable value immediately. However, the syntax you provided actually results in a runtime error or unexpected behavior because glob() expects a single string argument. If your code runs without a fatal error but returns an empty array, it is likely due to how the path is being interpreted relative to the web server's document root versus your application's actual working directory.

To fix this, you must ensure that the variable is treated as a string within the function call. You cannot wrap a variable in quotes like '$_SERVER[...] because that forces PHP to look for a literal string named $_SERVER, which does not exist. Instead, rely on string concatenation or the modern path joining helper functions provided by PHP.

Here is the correct implementation using standard concatenation:

$baseDir = $_SERVER["DOCUMENT_ROOT"] . '/myFolder';
$pattern = $baseDir . '/*';
$files = glob($pattern);

If you prefer a one-liner, ensure the quotes wrap only the literal string parts of the path, not the variable itself:

$files = glob($_SERVER["DOCUMENT_ROOT"] . '/myFolder/' . '*');

A more robust approach that handles OS-specific path separators and avoids manual concatenation is to use pathinfo() or dirname() if you are dealing with complex structures, but for simple appending, string concatenation remains the standard. However, be aware that $_SERVER["DOCUMENT_ROOT"] can be unreliable in certain hosting environments, particularly those running behind a reverse proxy or using specific configurations like Nginx where it might point to /var/www/html while your application is served from a subdirectory.

For high-security scenarios or when dealing with sensitive file operations like deleting files, relying on DOCUMENT_ROOT can introduce security risks if the path resolution is incorrect. It is often safer to define an absolute base path at the root of your configuration and use dirname(__DIR__) relative to that known location. This ensures that regardless of how the web server serves the content, your PHP application consistently knows its own file structure.

// Define a constant for the base folder to ensure consistency
define('BASE_PATH', dirname(__DIR__) . '/myFolder');

$files = glob(BASE_PATH . '/*');

This pattern decouples your file logic from the specific quirks of DOCUMENT_ROOT. When using this approach in a Laravel project, you might find that leveraging the framework's public directory or storage paths provides even more safety checks. For instance, if you are looking to implement similar logic within a Laravel application, you could reference resources managed through the service container or use the public_path() helper, which is often synonymous with DOCUMENT_ROOT but strictly validated by the framework.

Remember that deleting files based on a glob pattern without strict validation can lead to unintended consequences if the path resolution fails silently. Always verify that the resulting $files array contains only the expected entries before executing destructive operations like unlink().

Our network