File Creation Fails Despite Successful Execution
You are running into a classic PHP permission and path resolution issue. The code executes without throwing an immediate fatal error, which often misleads developers into thinking the logic is sound. However, the file simply isn't appearing in the pages directory because the operating system rejected the write operation silently or the path logic is flawed relative to how the script is invoked.
Understanding the Path Logic
The core issue lies in this line:$dir_path = "../pages/";
In your folder structure, create_page.php sits inside the admin directory. If you attempt to create a file at ../pages/something.txt, you are resolving the path relative to the current working directory (CWD) of the PHP process, not necessarily relative to where the script file physically resides.
When a web server executes a script via an HTTP request (e.g., http://yoursite/admin/create_page.php), the CWD is often set to the document root (root), not the directory containing the script. Consequently, ../pages/ resolves to /path/to/project/pages/, which might look correct visually but fails if the web server user lacks write permissions in that specific location.
Furthermore, your filename construction mixes relative paths with absolute expectations:$ourFileName = '../'.$page_name.".txt";
If $page_name is my_page1, you are trying to create a file named ../my_page1.txt. This places the file one level above the current working directory. If your CWD is root, this attempts to write to root/my_page1.txt. The resulting filename will be ../my_page1.txt literally, not the intended pages/my_page1.php. You also hardcoded .txt in the extension but later reference .php in your description; ensure the file extension matches the intent of the new file.
Permissions and Security Risks
Even if the path resolves correctly to ../pages/, the "no problem" execution message usually means PHP successfully opened the file handle, but the subsequent fwrite might fail silently depending on error reporting settings, or the directory simply doesn't exist yet.
Before attempting to write a file, you must verify two things:
- Directory Existence: The target directory (
../pages) must exist. If it does not,fopenwith mode'w'will fail immediately because it tries to create the file within a non-existent directory. You cannot use'w'to create directories; you would needmkdirfor that. - Write Permissions: The user running your web server (typically
www-data,apache, ornginxdepending on your stack) must have write permissions (chmod 755or higher directory, with the file writable by the group or owner).
A robust implementation should check these conditions before opening the file handle:
$targetDir = __DIR__ . '/../../pages';
// Or use realpath for absolute certainty based on current execution context
$targetDir = realpath(__DIR__ . '/../'); // Resolves relative to script location
if (!is_dir($targetDir)) {
// Optional: Create directory if missing, but be careful of security implications
if (!mkdir($targetDir, 0755, true)) {
die("Cannot create target directory.");
}
}
$fileName = basename($page_name) . '.php'; // Avoid creating literal '../' in filename
$fullPath = $targetDir . '/' . $fileName;
if (!is_writable(dirname($fullPath))) {
die("Write permission denied for the target directory.");
}
$ourFileContent = '<?php echo "something..."; ?>';
$ourFileHandle = fopen($fullPath, 'w');
if ($ourFileHandle) {
if (fwrite($ourFileHandle, $ourFileContent) === FALSE) {
echo "Cannot write to file ($fileName)";
exit;
}
} else {
die("can't open file: " . error_get_last()['message']);
}
fclose($ourFileHandle);
The Case for Modern Frameworks
While the fopen and file_put_contents approach works for legacy scripts, managing dynamic file creation in a production environment introduces significant security risks. Specifically, you are opening paths based on user input ($page_name) without sanitization. If an attacker controls $page_name, they could force the creation of a file with arbitrary content or even upload malicious scripts by targeting specific filenames like ../../config/database.php.
If you are building a larger application where dynamic page generation is required, this manual file I/O approach should be replaced by a database-driven architecture. Storing page content and metadata in a SQL database allows the framework to render views dynamically without touching the filesystem directly. This eliminates the risk of directory traversal attacks and ensures data integrity.
For projects migrating from procedural PHP or legacy systems where you need to persist templates, consider exploring modern Laravel packages that handle file system operations safely using the Storage facade. Resources like https://laravelcompany.com often discuss best practices for managing assets and filesystem interactions within the Laravel ecosystem, which can provide a safer abstraction over raw PHP file handling.
Final Verification
To debug this specific issue without changing code immediately, log the actual path being attempted:echo $ourFileName;
If it outputs something like ../my_page1.txt, you know your relative path logic is broken regarding the execution context. If it outputs a valid absolute path but the file still doesn't exist, check the error logs of your web server (Apache/Nginx) for permission denials from the www-data user.