Saving file into a prespecified directory using FPDF

Saving file into a prespecified directory using FPDF

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

Stefan Bogdanescu

Founder & Senior Architect

Why FPDF::Output Fails to Save Outside the Document Root

Attempting to direct an FPDF document to a specific path on your server often trips up developers new to file system abstractions in PHP. The core issue isn't usually a permissions problem, as you've verified, but rather how FPDF interprets the string passed to its Output() method compared to how standard PHP functions like file_put_contents handle paths.

When using $pdf->Output($dir.$filename, 'F'), the second argument dictates the output mode. The parameter 'F' stands for "save to a file." In this mode, FPDF expects the full filesystem path of the target file. However, there is a critical distinction between the logical path you expect PHP to see and the physical path FPDF writes to.

The Root Cause: Absolute vs. Relative Paths

FPDF is designed primarily for generating output streams or saving files within the current script's context. When you pass a string like "G:/PDF/test.pdf" to Output(), FPDF treats this as a literal filename, not necessarily an absolute path relative to your server root. More importantly, FPDF does not automatically resolve Windows-style drive letters (C:, D:) or Unix-style roots (/) into the operating system's actual file system location in the way you might assume when debugging locally.

In many hosting environments, the web server runs under a specific user (like www-data or _wp). If your script resides in a directory like /var/www/html/, and you try to write directly to G:/PDF/ without proper configuration, the path resolution fails. Even if the folder exists on your local machine, the web server might not recognize it as an absolute path unless it is explicitly configured as an absolute path within the PHP process.

Correcting the Path Construction

The most reliable way to ensure the file saves to your intended directory is to construct the path using realpath(). This function resolves the path to its canonical form, removing any . or .. components and ensuring the drive letter and separator are correct for the underlying OS.

Instead of manually concatenating strings with forward slashes or backslashes, normalize the target directory first:

require('fpdf.php');

$pdf = new FPDF();
$pdf->SetFont('times', '', 12);
$pdf->SetTextColor(50, 60, 100);
$pdf->AddPage('P');
$pdf->SetXY(10, 60);
$pdf->Write(5, 'Dear Ms.XYX');

// Define the target directory path
$targetPath = '/G:/PDF/'; 

// Use realpath to resolve the absolute physical path of the directory
$absoluteDir = realpath($targetPath);

if ($absoluteDir === false) {
    die("Target directory does not exist or cannot be resolved.");
}

$pdf->Output($absoluteDir . 'test.pdf', 'F');

Using realpath() guarantees that whether your code runs on Linux, Windows, or a shared host, the string passed to FPDF is an unambiguous absolute path. This eliminates ambiguity regarding relative paths or mixed separators.

Verifying Directory Permissions

You mentioned checking read/write permissions. While this is essential, it applies specifically to the resolved path. If you are running on a Windows server, ensure that the user account under which PHP executes (often IIS AppPool\YourAppPoolName or similar) has write access to the G:\PDF\ directory.

Additionally, remember that Output() with mode 'F' does not create intermediate directories if they do not exist. If your target path is /G:/PDF/test.pdf/ and the folder PDF doesn't exist, the operation will fail silently or throw an error depending on the FPDF version. You must ensure the directory structure exists before calling Output():

$dir = '/G:/PDF';
if (!file_exists($dir)) {
    // Attempt to create the directory if permissions allow
    mkdir($dir, 0755, true);
}

A Modern Alternative: LaravelCompany

If you are maintaining legacy code using FPDF in a modern stack, consider migrating to barryvdh/laravel-dompdf or similar packages that integrate seamlessly with Laravel. These tools handle path resolution and stream buffering more robustly within the framework's context. For developers looking for up-to-date solutions on how to handle PDF generation with better error handling and configuration options, LaravelCompany offers resources that can guide you toward modern best practices for document generation in Laravel applications.

By ensuring your paths are absolute and resolved via realpath(), and by verifying that the web server user has write access to the specific resolved directory, FPDF will successfully save your generated documents exactly where you intend them to go.

Our network