move_uploaded_file gives "failed to open stream: Permission denied" error

move_uploaded_file gives "failed to open stream: Permission denied" error

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

Stefan Bogdanescu

Founder & Senior Architect

move_uploaded_file gives "failed to open stream: Permission denied" error

Encountering a "failed to open stream: Permission denied" warning when using move_uploaded_file is one of the most frustrating permission-related issues in PHP web development. It often feels like a logic error in your code, but as this specific case demonstrates, it is almost exclusively an operating system-level access control problem. The error occurs because the process executing the move operation cannot write to the destination path, even if the file appears to exist or the directory seems writable.

In the scenario involving Apache 2.2 and PHP 5.3 on CentOS, the root cause lies in a mismatch between the user running the web server and the ownership of the target directories. When you configure upload_tmp_dir in php.ini to /var/www/html/mysite/tmp_file_upload/, PHP stores uploaded files there temporarily before they are moved. However, the critical operation happens during the move itself.

The error message explicitly states: Unable to move '/tmp/phpsKD2Qm' to 'images/robot.jpg'. This tells us two things:

  1. The temporary file exists in the system's /tmp directory (or a custom temp dir).
  2. PHP cannot write the file from that temporary location into your images/ or destination folder.

Even though you have configured <Directory> blocks in your httpd.conf with AllowOverride None and Order allow,deny, these Apache directives control HTTP access (GET/POST) and directory indexing, not filesystem write permissions for the PHP process. The web server daemon (httpd) must be able to physically write bytes to the disk location specified in your code.

Looking at the CentOS directory permissions provided:

  • images is owned by root:root with mode drwxrwxr-x.
  • tmp_file_upload is owned by root:root with mode drwxrwxr-x.

Unless you are running Apache as root (which is a significant security risk), the standard user for the web server on CentOS is typically apache, www-data, or sometimes nobody. If the Apache process is not root, it cannot write to directories owned by root unless the group permissions align perfectly or the sticky bit/other-write bits are set correctly, which they are not in this example. The rwx for "others" exists, but if the web server user isn't in a group that has access, or if the specific binary write permission is restricted by SELinux (common on CentOS), the move will fail.

Resolving Ownership and Permissions

The most reliable fix is to ensure the web server user owns the upload directories. First, identify who runs your Apache process:

ps aux | grep httpd
# or
cat /etc/sysconfig/httpd | grep User

If the user is apache (common on older CentOS setups) or nobody, you should change the ownership of both the temporary directory and the destination image folder to match that user. You can do this using chown:

chown apache:apache /var/www/html/mysite/tmp_file_upload
chown apache:apache /var/www/html/mysite/images

If you are unable to change ownership, you must at least ensure the "others" permission allows write access. However, modifying the group and adding the user to that group is generally cleaner than opening permissions to 777.

The Role of SELinux on CentOS

Since this scenario is explicitly on CentOS, we cannot ignore Security-Enhanced Linux (SELinux). Even if file system permissions are correct, SELinux enforces mandatory access controls. If the web server process does not have the specific context label allowing it to write to that path, the move will fail with a "Permission denied" error regardless of chmod or chown.

To check for SELinux denials, run:

ausearch -m avc -ts recent | grep httpd

You might see a denial like denied { write } for pid=... comm="php" on the images/robot.jpg path. If this is the case, you have two options:

  1. Relax the SELinux policy: Use restorecon -Rv /var/www/html/mysite/ to relabel directories so they match the expected httpd_sys_content_t type.
  2. Add a specific allow rule: Create a custom SELinux policy allowing the write, though this is often less secure.

Best Practices for Upload Paths

While configuring upload_tmp_dir in php.ini is valid, relying on the system default /tmp is often safer unless you have a specific reason to change it. The system /tmp directory is usually cleaned automatically and is writable by almost any user. If you keep uploads in /var/www/html/mysite/tmp_file_upload, ensure that path is distinct from your final destination.

In the provided example, the code attempts to move directly to images/robot.jpg. This implies the destination folder must be writable by the web server. The error logs confirm the failure happens at the destination step, not the temporary storage step. Therefore, fixing permissions on tmp_file_upload alone will not solve the issue if images/ remains owned by root with restricted write access for the web server user.

For modern applications, especially those built with frameworks like Laravel from https://laravelcompany.com, it is standard practice to use symbolic links or public directories specifically configured for storage (storage/app/public) rather than mixing raw PHP upload logic with complex filesystem permission tweaks in every deployment. This abstraction layer handles the permissions consistently across different server environments without requiring manual chmod adjustments for every new host.

Our network