file_put_contents: Failed to open stream, no such file or directory
You are encountering a classic PHP pathing error. The warning failed to open stream: No such file or directory in your stack trace points directly to the file_put_contents() function failing because the target directory does not exist on the filesystem. In your specific case, you are targeting /files/grantapps/NAME0.pdf, but PHP is looking for a directory literally named files/grantapps relative to your current working directory.
The code snippet provided uses relative paths:
file_put_contents("files/grantapps/" . $name . $i . ".pdf", $dompdf->output());
When you run this script via a web server, the "current working directory" is almost always the root of your public HTML folder (e.g., /home/username/public_html/subdir/). Therefore, PHP attempts to create the file at /home/username/public_html/subdir/files/grantapps/NAME0.pdf. If that files or grantapps subdirectory hasn't been created manually via SSH or a deployment script, the operation fails immediately.
Absolute vs. Relative Paths in Web Contexts
The confusion often stems from mixing absolute server paths with relative web paths. You mentioned trying the full directory path /home/username/public_html/subdir/files/grantapps/. While this is an absolute path, you must ensure two things:
- The user executing the script (often
www-dataornobody) has write permissions to that specific location. - The parent directories exist.
If you switch to an absolute path in your code, PHP will bypass the relative working directory entirely. However, hardcoding paths like /home/username/public_html... is brittle. If you deploy to a different server or change the user home directory, your script breaks. A more robust approach often involves using realpath() combined with configuration variables that point to the base of your application.
Verifying Directory Existence
Before attempting to write the PDF, it is defensive programming practice to verify the directory exists. You can use mkdir() to create the directory if it's missing, but only if you haven't already created it in a previous step or if the parent path doesn't exist.
Here is a safer pattern for handling the file generation:
require_once("dompdf_config.inc.php");
date_default_timezone_set('America/Detroit');
$html = 'code and whatnot is here';
$name = str_replace(" ", "", $_POST['form2name']);
$i = 0;
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
// Define the base directory relative to the script or use an absolute path configured in .env
$basePath = __DIR__ . '/files/grantapps';
// Ensure the directory exists before writing
if (!is_dir($basePath)) {
// Attempt to create it; set 0755 permissions so it's readable but not writable by everyone (security best practice)
mkdir($basePath, 0755, true);
}
// Find the next available filename
while (file_exists("$basePath/" . $name . $i . ".pdf")) {
$i++;
}
// Now write the file
file_put_contents("$basePath/" . $name . $i . ".pdf", $dompdf->output());
Handling Permissions and Ownership
Even if the directory exists, file_put_contents can fail due to permission issues. Web servers typically run as a specific user (e.g., www-data). If you created the folder manually on your local machine or via FTP with your own user permissions (755 for example), the web server might not have write access to that folder.
You need to ensure the directory is owned by the web server user or has group/world-write permissions (the latter being a security risk). On Linux systems, you would typically run:
sudo chown -R www-data:www-data /home/username/public_html/subdir/files/grantapps
Or adjust permissions to 775 if that fits your server's user/group structure.
Debugging the Exact Path
If you are still stuck, dump the exact path PHP is trying to access before calling file_put_contents. This clarifies whether the issue is a missing directory or an absolute path mismatch.
$targetPath = "files/grantapps/" . $name . $i . ".pdf";
echo "Target Path: " . realpath($targetPath) . "<br>";
echo "Base Dir Exists: " . (is_dir(dirname($targetPath)) ? 'Yes' : 'No') . "<br>";
By inspecting the resolved path, you can confirm if PHP is looking in /home/username/public_html/subdir/files/grantapps versus your intended location. If you intend to store files outside the public root for security reasons (e.g., to prevent direct HTTP access to PDFs), you must use an absolute path that points to a writable location outside public_html, rather than relying on relative paths from the script.
Security Note on File Generation
Generating files directly into a public_html subfolder allows anyone visiting /files/grantapps/NAME.pdf to download the file immediately after generation. This is often acceptable for PDF reports, but consider using Laravel's storage API if you are building on a framework like Laravel. Storing generated files in storage/app/public and linking them via a symbolic link or URL helper provides better separation of concerns and cleaner permission management compared to raw PHP file_put_contents.
