permission denied - php unlink

permission denied - php unlink

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

Stefan Bogdanescu

Founder & Senior Architect

Permission Denied - PHP Unlink

When you encounter Warning: unlink(test.txt) [function.unlink]: Permission denied in your logs, it is almost never a mystery regarding file permissions on the target file itself. In the scenario where both b.php and test.txt are owned by the same user and have 777 permissions, the issue lies squarely with the parent directory.

The operating system does not check permissions in isolation; it enforces a strict hierarchy known as the path traversal check. Before PHP can execute unlink($b), the kernel must verify that the script has execute permission (x) on every single directory component leading to the file's location. Even if the final file is writable and readable by everyone, the process cannot reach it without crossing the threshold of its containing folders.

Consider a typical web root structure where test.txt resides in /var/www/html/. If that directory is set to 755, the execute bit is present for the owner (rwx), group (rx), and others (rx). However, if any parent directory higher up the chain—such as /var/www or even /—lacks the execute permission for your web server user, the operation fails. The 777 on the target file is irrelevant because the process was blocked before it could touch the inode.

The workaround you identified—setting the entire directory tree to 777 to make unlink work, then reverting it later—is a classic symptom of misunderstanding the permission chain. While changing the parent directory to 777 allows the operation to proceed, it introduces significant security risks. Giving world-writable and world-executable access to your web root is a primary vector for path traversal attacks and directory listing exploits.

To resolve this without compromising the entire environment, you must audit the full path of the file being deleted. You can do this by inspecting the complete pathname or ensuring that every parent directory in the chain explicitly grants execute permission to the others category. For instance, if your file is located at /home/user/project/assets/test.txt, and /home/user is 755 but /home is 750, the web server user might lack the necessary traversal rights depending on how the group memberships align with the running user of PHP.

In a production environment using Laravel, you should rarely find yourself manually toggling permissions back and forth. Instead, configure your deployment process or CI/CD pipeline to ensure that only the specific directories required for execution have the correct bits set. If you are dealing with legacy systems or shared hosting where you cannot modify parent directories, you may need to place files in a subdirectory under your control where you can grant full permissions locally without exposing the root directory structure.

The most robust solution is often architectural: ensure that the web server user (commonly www-data or apache) matches the owner of the application files, rather than relying on world-writable directories. If your PHP process runs as a specific system user, verify that this user has read and execute access to all intermediate directories in the file path.

There is no magic flag to bypass the OS permission hierarchy. The error message is literal: the kernel denied access at the point of traversal. Fixing it requires aligning the directory tree's permissions with the requirements of the executing process, not just the target file. If you are building a secure application, remember that 777 should be treated as an emergency override, not a standard configuration state.

Our network