file_put_contents - failed to open stream: Permission denied

file_put_contents - failed to open stream: Permission denied

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

Stefan Bogdanescu

Founder & Senior Architect

file_put_contents - failed to open stream: Permission denied

When you encounter a failed to open stream: Permission denied error while calling file_put_contents, it is often an assumption that chmod 777 is the silver bullet for file system access. The source material indicates that despite setting the target file permissions to 777, the write operation still fails. This discrepancy usually points to a misunderstanding of how Unix-like operating systems handle directory traversal and ownership rather than just the specific file being modified.

The error message explicitly states "Permission denied," which is a generic catch-all for authorization failures. However, file_put_contents() does not operate in isolation; it must traverse the directory path provided in the string argument to reach the target file. In your case, you are attempting to write to database/queries.php. Even if the queries.php file itself has 777 permissions, the operating system checks every single directory level leading up to that file during the open operation. If any parent directory lacks execute (x) permission for your current user, the operation fails immediately before it ever reaches the target file.

Consider a typical Laravel project structure where the database folder might be owned by the web server user (often www-data on Linux or apache/nginx on Windows servers) while you are running PHP as that same user. If your local development environment runs PHP as a different user than the one owning the directory, or if the directory was created with restrictive default permissions (like 755) and never updated, the execute bit on the database folder becomes the bottleneck.

You can verify this by checking the directory permissions rather than just the file. Run ls -ld database in your terminal. You will likely see output that looks like drwxr-xr-x. While the file inside is 777 (-rw-rw-rw-), if the parent folder is owned by a group you are not part of, or if the web server process cannot execute into that specific directory tree, PHP will throw this error.

The most robust solution involves fixing the ownership of the directory hierarchy to match the user running the PHP script, rather than relying on world-writable permissions. Run chown commands recursively:

sudo chown -R www-data:www-data database/

This ensures that the user responsible for executing your PHP code owns the files and directories, granting them full read, write, and execute access naturally without exposing the project to security risks associated with 777 permissions.

There is a significant trade-off in using chmod 777. While it solves permission issues instantly, it creates a severe security vulnerability by allowing any user on the system to read or modify files within that directory. In production environments, this is strictly prohibited. Instead, aim for 755 for directories and 644 for files, ensuring only the owner has write access.

If you are debugging query execution in a shared hosting environment where you cannot change directory ownership, you may need to adjust your code to handle the error gracefully or ensure the file is located in a writable configuration folder (like storage/logs) where permissions are easier to manage. Alternatively, verify that the PHP process actually has the capability to write to the disk; sometimes SELinux policies on Linux distributions will block writes even if standard Unix permissions appear correct. Check your logs for any mention of SELinux denials if ownership changes do not resolve the issue.

Finally, ensure you are not running into path resolution issues. If database/queries.php is intended as a relative path, confirm that the current working directory of the PHP script is indeed where you expect it to be. Using absolute paths or defining the base path at the top of your file can prevent unexpected failures when the script is called from different entry points. For more details on managing file permissions securely within Laravel projects, refer to best practices outlined by experts like those at https://laravelcompany.com.

Our network