Codeigniter - dynamically getting relative/absolute path outside of application folder

Codeigniter - dynamically getting relative/absolute path outside of application folder

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

Stefan Bogdanescu

Founder & Senior Architect

Escaping CodeIgniter's Application Root for External Temporary Storage

Working within a PHP framework often feels like being inside a walled garden. The application code lives in public/, sits on top of the system libraries, and relies on defined constants like APPPATH and SYSTEMPATH. When you need to operate outside these boundaries—specifically at the same level as your application folder rather than nested within it—the standard path resolution tools provided by CodeIgniter start hitting a wall.

The scenario is common: you are fetching a user's Facebook profile image, need to perform some temporary processing or renaming, and then move it to a permanent storage location. That intermediate step requires a directory adjacent to your app root, perhaps called temp_external. If you try to use __DIR__, __FILE__, or even realpath(APPPATH), you will invariably get paths that point into the application directory itself. You need a way to navigate up one level and construct an absolute path without hardcoding the server's base directory string.

The core issue is that framework constants are relative to the framework installation, not the server root. To solve this, you must leverage PHP's built-in path manipulation functions to traverse the filesystem hierarchy dynamically. The most reliable approach involves using dirname() combined with realpath().

First, identify where your current script resides. If you are inside a controller or model within CodeIgniter, $this->config->base_url gives you the web root, but that doesn't help with server-side file paths. Instead, look at how the framework defines its own location. In many CodeIgniter setups, the APPPATH constant points to the application folder (e.g., /var/www/project/app). If your target directory needs to be at the parent level of this folder, you can use dirname() on that constant:

$applicationPath = APPPATH; 
// Example result: /var/www/project/app

$parentPath = dirname($applicationPath);
// Example result: /var/www/project

Once you have the parent path, wrap it with realpath(). This function resolves any symbolic links and ensures you are working with a canonical absolute path. This is critical when dealing with temporary directories that might be mounted via bind mounts or symlinks in a Docker environment; relying solely on relative strings can lead to file write failures if the mount point changes.

$tempDir = realpath(dirname(APPPATH)); 
// Result: /var/www/project (absolute, canonical)

With this absolute path in hand, you can safely append your temporary directory name.

$externalTempPath = $tempDir . '/temp_external';

Before attempting to save files there, you must ensure the directory exists and has the correct permissions. CodeIgniter does not provide a built-in constant for this external path, so you should create a helper method or a small utility function within your project to encapsulate this logic. This prevents every developer on the team from guessing how many ../ are needed if the application is moved up or down in the directory tree.

public static function getExternalTempPath() {
    $basePath = dirname(APPPATH);
    return realpath($basePath . '/temp_external');
}

There is a significant trade-off to consider here regarding security and deployment. Storing temporary files outside the web root (if your application folder is directly accessible via index.php) is generally safer than placing them in system/ or application/cache. However, if your server configuration allows direct access to the parent directory without passing through the PHP entry point, you must ensure that the temp_external folder has restricted permissions.

Using file_put_contents() with a path outside the application requires careful validation of the input data before writing. Since you are fetching external images (like from Facebook), always verify the content type and validate the filename to prevent directory traversal attacks or overwriting critical system files if your web server configuration is misaligned.

While CodeIgniter excels at managing paths within its own structure, stepping outside requires a bit of manual filesystem navigation. By dynamically resolving the parent directory of APPPATH using realpath(), you gain a robust, portable solution that works regardless of where the application is hosted or how the web server is configured. This pattern eliminates the need for hardcoded absolute paths and ensures your temporary storage logic remains decoupled from the specific layout of your development machine.

Our network