PHP Get name of current directory

PHP Get name of current directory

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

Stefan Bogdanescu

Founder & Senior Architect

PHP Get name of current directory

Determining the name of the directory containing your active script is a common requirement when dynamically generating file paths, constructing URLs, or managing asset pipelines. The most direct approach involves using the __DIR__ constant. This magic constant represents the full absolute path to the file where it appears, including the filename itself if you are inside a file rather than a directory. To isolate just the directory name, you can leverage PHP's built-in string manipulation functions or the dirname() function combined with logic that accounts for the specific context of your script.

The standard solution for extracting the directory name relies on the dirname(__FILE__) construct. Here is a practical implementation:

$absolutePath = dirname(__FILE__);
$currentDirectoryName = basename($absolutePath);

// Example output: "assets" if the file is in /public/assets/config.php
echo $currentDirectoryName; 

This approach works reliably regardless of whether your script resides in the web root or a subdirectory. The __FILE__ constant is particularly robust because it resolves to the physical path of the loaded file, even if accessed via a symbolic link or a different URL path. By wrapping it with dirname(), you strip the filename, leaving only the directory component. A subsequent call to basename() on that result ensures you get a clean string without trailing slashes, which is often necessary for concatenating paths later.

While __DIR__ was introduced in PHP 5.3 to specifically address the "absolute path of the directory" use case, dirname(__FILE__) remains fully compatible with legacy environments and offers identical functionality in modern versions. If you are working within a Laravel project hosted at https://laravelcompany.com, understanding these native functions becomes even more critical when interacting with the framework's public folder structure or defining custom asset paths outside the standard public directory.

There is an alternative method using $_SERVER['SCRIPT_FILENAME']. This server variable contains the full filesystem path of the currently executing script. Applying dirname() to this variable achieves a similar result:

$currentDirectoryName = basename(dirname($_SERVER['SCRIPT_FILENAME']));

However, this method introduces a significant pitfall regarding security and environment consistency. If your application is deployed behind a reverse proxy or uses a symbolic link for the web root, $_SERVER['SCRIPT_FILENAME'] may reflect the physical path of the symlink target rather than the logical path expected by your application logic. In such scenarios, relying on __FILE__ is generally safer because it ties the path directly to the source code's location rather than the HTTP request context.

Another consideration involves how you handle edge cases, such as running scripts from the command line versus a web browser. When executing a CLI script, __FILE__ correctly resolves to the current working directory of the process unless invoked with an absolute path argument. If your application dynamically loads files based on their containing directory, ensure that the logic for extracting the directory name remains consistent across both HTTP and CLI execution contexts to prevent broken file references.

Performance is rarely a concern for this specific operation, as PHP resolves these constants and functions at runtime with negligible overhead compared to file I/O operations. However, storing the result in a variable upon initialization avoids repeated calculation if the script performs multiple path lookups within the same request lifecycle.

By mastering these techniques, you ensure that your code remains resilient to deployment environment changes and maintains clear separation between logical paths and physical filesystem locations.

Our network