Permission denied when opening or creating files with PHP

Permission denied when opening or creating files with PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Permission denied when opening or creating files with PHP

Encountering a Permission denied error in PHP usually signals a mismatch between what your script attempts and what the underlying operating system allows. When you see the cascade of warnings where fwrite() expects a resource but receives a boolean, it means fopen() failed before execution could proceed to writing. The root cause is rarely within the PHP code itself; instead, it lies in the Unix/Linux filesystem permissions governing the web server process.

In the scenario described, the error originates from /web/com/example.com/index.php. This path indicates the script is running under a specific context, likely managed by an FTP client like net2ftp. Even though you can upload files via that interface, the ability to create or modify them depends on who owns the file and what group permissions are assigned.

Understanding the Filesystem Context

When PHP executes fopen("test.txt", "w"), the web server process (typically running as user www-data, apache, or nobody) attempts to access the file. The failure occurs if that specific user lacks write privileges for the parent directory or the file itself.

The most common pitfall is assuming that because you can upload a file via FTP, you have full control. Uploading creates the file with the permissions of your FTP login user (e.g., ftpuser). If your PHP script runs as www-data, and www-data cannot write to files owned by ftpuser, or if the directory lacks group/world write access, the operation fails.

Diagnosing Ownership and Permissions

To resolve this, you must inspect the current state of the file system. Use ls -la in your terminal to view ownership and permission bits:

ls -la /web/com/example.com/

You are looking for two distinct issues:

  1. Owner Mismatch: The file or directory might be owned by a user different from the one running PHP.
  2. Permission Bits: The numeric permissions (like 644 or 755) must allow the web server process to write.

If the files are owned by your FTP user (ftpuser) and you cannot access them via PHP, simply changing ownership might be risky if it affects other scripts or security policies. Instead, focus on adjusting permissions to grant access to the web server group.

Corrective Actions

Adjusting Permissions

You can modify file permissions using chmod. A common approach is to set the directory to 775 and files to 664, assuming both your FTP user and the web server belong to the same group (often named www-data).

# Make the directory writable by the group
chmod 775 /web/com/example.com/

# Make files writable by the group
chmod 664 /web/com/example.com/test.txt

This allows the web server process to write to the file without transferring ownership. However, this assumes your FTP user and the web server share a common group. If they do not, you may need to add the web server user to your FTP user's group using usermod -aG ftpuser_name www-data.

Fixing Ownership

If permission adjustments fail because of strict security policies preventing group writes, transferring ownership is the direct solution. This should be done carefully, as it grants broad access to that specific directory.

# Change owner to the web server user (e.g., www-data)
chown -R www-data:www-data /web/com/example.com/

This command recursively changes both the owner and group of all files and directories in that location. While this solves the immediate fopen failure, it means any file created there will now be owned by the web server. This is generally safe for writable assets but requires monitoring to ensure sensitive configuration files aren't left world-writable.

Security Considerations

While granting write permissions solves the error, it introduces a security surface area. If test.txt becomes a target for malicious uploads or script injection, having wide-open write permissions amplifies the risk.

In production environments, consider implementing strict ownership where only specific directories are writable by the web server. For dynamic file creation, ensure that the application logic validates content before writing. If you are using a framework like Laravel, the underlying principle remains identical: the storage disk must be mounted with permissions that allow the php worker to write, often configured via storage/app/public permissions in .env deployment guides found on sites like https://laravelcompany.com.

Ultimately, the error is a boundary condition between your web application logic and the operating system's access control list. Resolving it requires aligning the identity of the PHP process with the rights granted to the filesystem resources it touches.

Our network