move_uploaded_file failed to open stream: Permission denied - Mac

move_uploaded_file failed to open stream: Permission denied - Mac

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

Stefan Bogdanescu

Founder & Senior Architect

move_uploaded_file failed to open stream: Permission denied - Mac

When debugging a move_uploaded_file error on macOS, specifically encountering "failed to open stream: Permission denied," it is crucial to understand that this is rarely an issue with the PHP script logic itself. The provided code snippet correctly validates the upload using is_uploaded_file() before attempting the move. If that validation passes, the file is genuinely a temporary upload artifact residing in /tmp. The failure occurs at the filesystem level when PHP tries to write or overwrite the target directory (images/).

The error message points directly to the destination path: images/VIDEO_TS.VOB. The root cause is almost invariably the operating system's file permissions, not the web server configuration. In your specific scenario involving XAMPP on macOS Yosemite, you are dealing with a clash between the user running the PHP process and the user owning the target directory.

The User Context Mismatch

On macOS, standard development environments like XAMPP often run the Apache/PHP services under a system account (historically nobody or _appserver depending on the version) to isolate processes from your primary user account. When you attempt to move an uploaded file into a directory located within your home folder's project structure (e.g., /Applications/XAMPP/xamppfiles/htdocs...), you are implicitly asking that system service account to write to a directory owned by your standard user.

By default, macOS directories are owned by root or your specific user with restrictive permissions (usually 755 or 700). The system service account lacks write access to these locations. Your attempt to use sudo chown nobody actually exacerbates the problem in many cases. If you explicitly change ownership of the entire directory tree to nobody, but the parent directories still have restrictive permissions, or if the specific images folder is not writable by the service account, the move fails.

Furthermore, running chmod 775 on a single file (do_upload.php) is functionally useless for solving this issue. File permissions apply to the directory structure where the file resides and the target destination, not the script itself (unless you are dealing with SELinux/AppArmor contexts, which macOS does not use in this manner). You need to modify the attributes of the images directory and its parent folders up the chain to /htdocs.

Correcting Directory Permissions

To resolve the "Permission denied" error without relying on sudo, you must align the permissions so that the web server process can write to the destination. The most robust solution involves adjusting the ownership and mode of the specific upload directory.

Instead of targeting individual files or using sudo indiscriminately, identify the exact path where the file is being moved. If your project lives in /Applications/XAMPP/xamppfiles/htdocs/Week3/Lesson2, you should ensure the images folder allows group write access or is owned by a user the service can access.

Run the following commands in Terminal to grant the necessary read/write/execute permissions to the group, assuming your XAMPP installation uses a standard setup:

# Navigate to your project root first
cd /Applications/XAMPP/xamppfiles/htdocs/Week3/Lesson2

# Ensure the images directory exists
mkdir -p images

# Set ownership to your current user and group (often necessary if you can't switch service users)
chown -R $(whoami):staff images/

# Grant read, write, and execute permissions to owner and group
chmod -R 775 images/

If you must use sudo because the directory is owned by root, do so carefully:

sudo chown -R nobody:staff /Applications/XAMPP/xamppfiles/htdocs/Week3/Lesson2/images
sudo chmod -R 775 /Applications/XAMPP/xamppfiles/htdocs/Week3/Lesson2/images

Note the distinction between changing the file permissions and directory permissions. The images folder must have execute (x) bits set for both owner and group, as the filesystem treats a path with an execute bit as traversable. Without the execute bit on the parent directories, the OS cannot "enter" them to reach the file inside, regardless of write permissions.

Security Considerations

While fixing this error allows your application to function, blindly setting folders to 775 introduces security risks. This permission mode allows any user in the group to modify files within that directory. If your images folder contains sensitive data or executable scripts, this could be a vector for privilege escalation.

A better approach for production environments is to set the ownership of the upload directory specifically to the web server user (often _appserver on modern macOS XAMPP installs) rather than a generic group. You should verify which user Apache is actually running as by checking /private/var/log/apache2/error.log or looking at the ServerAdmin and user configurations in httpd.conf.

Additionally, consider moving uploaded files to a location outside your source control repository immediately after processing. Storing large binary files like VIDEO_TS.VOB directly alongside PHP code is inefficient and risky. Using a dedicated storage bucket (like AWS S3) or a separate volume mounted with appropriate permissions can decouple the web server's write needs from your development environment's security model.

If you are building a larger application, consider leveraging Laravel's filesystem abstraction. The framework provides robust methods for handling file uploads that automatically manage temporary files and disk permissions, reducing the likelihood of encountering these low-level stream errors. For more details on secure file handling in modern PHP stacks, resources like https://laravelcompany.com often provide best practices on managing storage drivers securely.

Our network