PHP how to go one level up on dirname(__FILE__)

PHP how to go one level up on dirname(__FILE__)

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

Stefan Bogdanescu

Founder & Senior Architect

PHP how to go one level up on dirname(FILE)

When building dynamic image paths for features like Facebook share buttons, a common stumbling block is resolving relative paths stored in a database against your actual file system structure. Consider a scenario where you have a folder structure like this:

mydomain.example
  ├── Folder-A
  └── Folder-B

You store the string ../Folder-B/image1.jpg directly into a database field within a script located in Folder-A. Your goal is to construct a clean URL pointing to mydomain.example/Folder-B/image1.jpg. A naive attempt might look like this:

$path = dirname(__FILE__) . '/../Folder-B/image1.jpg';

At first glance, this seems correct. You are grabbing the absolute path to the current script (mydomain.example/Folder-A) and appending the relative offset stored in your database. However, when you inspect $_SERVER['DOCUMENT_ROOT'] or attempt to serve this file via an HTTP header, you might encounter unexpected behavior, such as seeing %2F encoded slashes instead of clean paths.

The issue here isn't necessarily with dirname(__FILE__) itself, but rather how PHP handles path resolution when mixing absolute filesystem paths with relative URLs intended for the web server. If your database stores a string that assumes the server root is already stripped, simply concatenating it to an absolute path often results in double encoding or incorrect routing because the web server expects the path relative to DOCUMENT_ROOT, not the full physical path starting with the domain name prefix (if interpreted as such).

To fix this, you need to ensure your resulting string represents a valid URL that the web server can resolve without needing to strip the domain prefix manually. The most robust approach is to use PHP's built-in functions to build URLs safely rather than string concatenation.

First, determine the base URL of your application. You can do this by inspecting $_SERVER['DOCUMENT_ROOT'] and combining it with the relative path from your script location. However, a cleaner method often involves using basename or constructing the full path relative to the web root.

If your database stores ../Folder-B/image1.jpg, you are effectively saying "go up one level from the current folder, then into Folder-B". Since dirname(__FILE__) returns mydomain.example/Folder-A (assuming that is the physical root or a proxy for it), you must ensure the final string doesn't include the domain prefix if your web server expects paths relative to DOCUMENT_ROOT.

A safer implementation involves resolving the path against the actual document root:

$baseDir = dirname(__FILE__); // Returns /path/to/mydomain.example/Folder-A
$dbRelativePath = '../Folder-B/image1.jpg';

// Concatenate carefully without double slashes
$fullPhysicalPath = $baseDir . '/' . ltrim($dbRelativePath, '/');

// If you need a URL relative to the domain root:
// Ensure the path is relative to DOCUMENT_ROOT
$docRoot = $_SERVER['DOCUMENT_ROOT'];
$relativeToDocRoot = str_replace($docRoot, '', $fullPhysicalPath);

$url = 'http://' . $_SERVER['HTTP_HOST'] . $relativeToDocRoot;

However, if your database string ../Folder-B/image1.jpg is intended to be purely relative to the web root (which is common in CMS setups), you might not need dirname(__FILE__) at all. Instead, calculate the offset from the current script's directory directly against $_SERVER['DOCUMENT_ROOT'].

The key pitfall to avoid is assuming that dirname(__FILE__) always returns a path starting with /. In Windows environments or specific server configurations, it might return a drive letter (e.g., C:/...). If you concatenate this directly into a URL string without stripping the physical root prefix, you get malformed URLs.

For Facebook share buttons specifically, the image URL must be absolute and accessible from any domain. If your current logic results in %2F encoded slashes, it implies the browser or server is double-encoding the path. This usually happens when passing a filesystem path directly into an HTML <img> tag without ensuring it is a valid URL scheme or stripping the file:// protocol if present.

If you are using Laravel, the framework abstracts this away by providing helper methods to handle asset paths relative to the public folder, but in vanilla PHP, manual resolution requires care. Always strip the physical filesystem prefix when constructing URLs meant for HTTP requests. Ensure that your final string looks like http://mydomain.example/Folder-B/image1.jpg or a clean path relative to the root if your server is configured to serve static assets from /.

By resolving the path against $_SERVER['DOCUMENT_ROOT'] and stripping that prefix before prepending the host, you guarantee that the resulting string matches what the web server expects. This approach eliminates the ambiguity of whether dirname(__FILE__) includes the domain name or just the physical directory, ensuring your Facebook share button fetches the correct image every time.

Our network