[php] PHP fopen() Error: failed to open stream: Permission denied

I learning how to write a WordPress plugin. I need some help writing some data to an XML file. I'm on my local machine, a Mac running MAMP. I have PHP 5.2.13. In my plugin, I've got:

$file_handle = fopen('markers.xml', 'w');
$stringdata = "Test Info";
fwrite($file_handle, $stringdata);
fclose($file_handle);

Running the above gives me the following error:

Warning: fopen(markers.xml) [function.fopen]: failed to open stream: Permission denied in /Users/my_name/Sites/my_site/wp-content/plugins/my_plugin_folder/my_plugin_main_file.php on line 73

Warning: fwrite(): supplied argument is not a valid stream resource in /Users/my_name/Sites/my_site/wp-content/plugins/my_plugin_folder/my_plugin_main_file.php on line 75

Warning: fclose(): supplied argument is not a valid stream resource in /Users/my_name/Sites/my_site/wp-content/plugins/my_plugin_folder/my_plugin_main_file.php on line 76

I tried using the absolute path in the $file_handle line: http://my_site/wp-content/plugins/my_plugin_folder/markers.xml. But, that didn't work.

I also tried changing the permissions on markers.xml as follows:

(Me): Read & Write (unknown): Read only everyone: Read & Write

For some reason, my Mac wouldn't let me change (unknown) to Read & Write. I'm not sure if that makes a difference. I right-clicked on the file and selected 'Get Info' in order to change the permissions.

In phpInfo(), I've got:

"Registered PHP Streams https, ftps, compress.zlib, compress.bzip2, php, file, data, http, ftp"

Is a WordPress setting causing the problem? or is it just PHP issue?

Any suggestions on how to solve this problem?

Thank you.

This question is related to php wordpress fopen permission-denied

The answer is


You may need to change the permissions as an administrator. Open up terminal on your Mac and then open the directory that markers.xml is located in. Then type:

sudo chmod 777 markers.xml

You may be prompted for a password. Also, it could be the directories that don't allow full access. I'm not familiar with WordPress, so you may have to change the permission of each directory moving upward to the mysite directory.


[function.fopen]: failed to open stream

If you have access to your php.ini file, try enabling Fopen. Find the respective line and set it to be "on": & if in wp e.g localhost/wordpress/function.fopen in the php.ini :

allow_url_fopen = off
should bee this 
allow_url_fopen = On

And add this line below it:
allow_url_include = off
should bee this 
allow_url_include = on

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to wordpress

#1273 – Unknown collation: ‘utf8mb4_unicode_520_ci’ How to get WooCommerce order details Wordpress plugin install: Could not create directory WooCommerce: Finding the products in database How to get post slug from post in WordPress? How to get featured image of a product in woocommerce Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\wordpress\wp-includes\class-http.php on line 1610 Use .htaccess to redirect HTTP to HTTPs Load More Posts Ajax Button in WordPress How to decode encrypted wordpress admin password?

Examples related to fopen

PHP - Failed to open stream : No such file or directory Getting an error "fopen': This function or variable may be unsafe." when compling Difference between r+ and w+ in fopen() Create a file if one doesn't exist - C PHP php_network_getaddresses: getaddrinfo failed: No such host is known PHP fopen() Error: failed to open stream: Permission denied Writing a new line to file in PHP (line feed) Unable to open a file with fopen() C fopen vs open fopen deprecated warning

Examples related to permission-denied

Error: EACCES: permission denied, access '/usr/local/lib/node_modules' PermissionError: [Errno 13] Permission denied Github permission denied: ssh add agent has no identities WinSCP: Permission denied. Error code: 3 Error message from server: Permission denied Permission denied on accessing host directory in Docker .bashrc: Permission denied C compile error: Id returned 1 exit status Executing a shell script from a PHP script Permission denied on CopyFile in VBS Node.js EACCES error when listening on most ports