PHP: move_uploaded_file(): Unable to move '/
When move_uploaded_file() throws a "Permission denied" error, it is almost never an issue of PHP syntax or logic within your application code. The function operates by moving a file from the system's temporary directory (typically /tmp on Linux) to your designated upload path. If the process fails immediately upon attempt, the culprit is invariably the operating system's file permissions or the user context under which your web server is running.
The error logs you are seeing explicitly state failed to open stream: Permission denied. This tells us that the specific user account executing the PHP script lacks the necessary rights to write to the target directory. Simply setting a directory to 777 (rwx for owner, group, and others) is a reactive measure that often fails because it does not address the fundamental mismatch between the web server user and the file ownership.
Understanding the Web Server User
To resolve this, you must identify which user account your web server process runs as. On most Linux distributions using Apache or Nginx with PHP-FPM, this is not root (which would be a massive security risk) and is rarely your personal login user. It is typically www-data, apache, nginx, or nobody.
You can verify the current user by checking your web server configuration files or looking at the process list:
ps aux | grep php
# or
ls -ld /var/www/admin/_application/models/Roles.model
If your application runs as www-data, attempting to move a file into a directory owned by your personal user (or vice versa) without explicit permissions will trigger the exact error you are experiencing. The web server process cannot write to a folder it does not own, regardless of whether that folder is set to 777 globally if the underlying inode flags prevent it (though this is less common on modern filesystems).
Correct Permission Strategy
Setting permissions to 777 is widely discouraged in production environments due to security vulnerabilities. If any user or service can write anywhere, your application becomes susceptible to remote code execution attacks. Instead, follow a three-step ownership and permission model:
- Identify the Web Server User: Determine if it is
www-dataor another account. - Change Ownership (chown): Make the web server user the owner of the upload directory and any files within it.
- Set Permissions (chmod): Grant read, write, and execute permissions to the owner (
755) and read/write/execute to the group or others as needed for your specific application logic, usually755for directories and644for static files.
Here is how you should secure the directory at /var/www/admin/uploads:
# Change ownership to the web server user (example: www-data)
sudo chown -R www-data:www-data /var/www/admin/uploads
# Set directory permissions: owner can read/write/execute, others can read/execute
sudo chmod -R 755 /var/www/admin/uploads
# Ensure the parent directory exists and has correct links
ls -ld /var/www/admin
Verifying the Temporary Directory
The error message also references a source file located in /tmp. While less common, move_uploaded_file() can fail if the process does not have write permissions to the system's temporary directory. In some hardened environments or specific container configurations, /tmp might be mounted with no-write access for standard users.
You should ensure that the web server user has write access to /tmp:
ls -ld /tmp
# Expected output typically shows drwxrwxrwt (sticky bit set) owned by root:root
# However, the sticky bit allows any owner of a file in /tmp to delete/move it.
# The web server user usually needs write access here too if files are being moved from there.
If you are running this on a fresh server setup where /tmp is owned by root but has strict permissions, and your PHP process cannot write there, the solution is to ensure the directory mode allows writes for the specific group or user, though standard practice usually relies on the sticky bit. If the error persists after fixing the destination folder, investigate if the source temp file was created successfully but with a permission set that prevents the move operation.
Debugging Ownership Issues
If you are still unsure which path needs correction, use find to locate files owned by incorrect users within your upload directory:
find /var/www/admin/uploads -user $USER # Replace $USER with the web server user
# Example: find /var/www/admin/uploads -user nobody
Iteratively change ownership of directories that show up as belonging to a non-web-user. Remember that chown only affects files and directories explicitly listed; recursive operations (-R) are essential for nested upload structures.
By aligning the ownership of your upload directory with the user executing the PHP script, you eliminate the "Permission denied" error without compromising server security through overly permissive 777 settings.