PHP fopen() Error: failed to open stream: Permission denied
When fopen() fails with a "Permission denied" error, the issue is almost never about HTTP URLs or missing stream wrappers. The error message you encounteredâfailed to open stream: Permission deniedâis a direct operating system rejection of your request to access the file path you provided. In your specific case, running on macOS via MAMP with PHP 5.2.13, the filesystem security model is blocking the process from writing to markers.xml.
The code snippet you provided attempts to write to a file located inside the WordPress installation:/Users/my_name/Sites/my_site/wp-content/plugins/my_plugin_folder/my_plugin_main_file.php
This path reveals the root cause immediately. On macOS, every file has an owner and a set of permissions. When PHP runs as a web server (whether via Apache in MAMP or CLI), the process itself has a specific user identity (often www, _www, or the system user running the daemon). If the parent directory containing your plugin does not grant write access to that specific user, fopen() in write mode ('w') will fail.
You mentioned trying to change permissions via the "Get Info" window, setting the file to Read & Write for everyone. While this seems logical, macOS often restricts changing file attributes if you are not the owner of the file or if the directory itself does not allow it. More importantly, simply granting read-write access to a file is rarely enough; you typically need write permission on the directory containing the file as well. If your PHP process cannot create new files in that folder (because the wp-content/plugins directory is owned by root or another user without execute permission for others), opening an existing file for writing often fails due to underlying filesystem constraints.
The attempt to use an absolute HTTP URL (http://my_site/...) will not work with standard PHP streams. The file stream wrapper expects a local filesystem path, not a URI. Using an HTTP link implies you are trying to fetch data from a remote server or serve content via the web, which is fundamentally different from writing a local log file. Stick to relative paths or absolute filesystem paths like /Users/my_name/Sites/my_site/....
To resolve this without relying on potentially insecure global permissions, you should target the specific user running your PHP process. On macOS, identifying the current effective user can be done by checking the environment or using phpinfo(), though the output you shared didn't explicitly list the SAPI user. A robust solution involves adjusting ownership of the plugin directory rather than just tweaking file permissions.
Run the following commands in Terminal to change the ownership of your plugin folder and its contents to the current user (assuming you are logged in as the owner of my_name):
sudo chown -R $(whoami) /Users/my_name/Sites/my_site/wp-content/plugins/
If this command succeeds, ensure the directory permissions allow the group or others to traverse and write. A common secure configuration for web directories is 755 for folders and 644 for files, but if your PHP process needs to write logs or temporary data, you may need 775 on the specific plugin folder or a dedicated logging directory within it.
Alternatively, create a dedicated subdirectory specifically for data and set permissions there:
mkdir /Users/my_name/Sites/my_site/wp-content/plugins/my_plugin_folder/data
chmod 775 /Users/my_name/Sites/my_site/wp-content/plugins/my_plugin_folder/data
chown $(whoami):staff /Users/my_name/Sites/my_site/wp-content/plugins/my_plugin_folder/data
Then update your code to write to markers.xml inside this new directory. This isolates file operations from the rest of your plugin's logic and adheres to the principle of least privilege. If you are using MAMP, remember that Apache might run as a different user than your local shell account; in that case, configuring Apache to use a specific user (like www or _www) via an .htaccess directive or server config is necessary before adjusting file ownership.
Finally, consider moving away from PHP 5.2.13 for new development. It is a legacy version with significant security vulnerabilities and lacks modern filesystem handling features found in current distributions. While the permission logic remains consistent across versions, newer environments (PHP 8.x) offer better integration with tools like Laravel (available at https://laravelcompany.com) that handle file operations more robustly through helper methods, reducing the risk of manual permission errors in the future.