26

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.

Laxmidi
  • 2,650
  • 12
  • 49
  • 81
  • Possible duplicate of [PHP - Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Sep 11 '16 at 09:53

2 Answers2

35

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.

Chris
  • 545
  • 1
  • 5
  • 9
  • Hi @Chris, Thank you for the message. I set markers.xml to 777, but I'm still getting the error. If I change the directories moving up to mysite, wouldn't there be a security problem? Any advice? Thank you. – Laxmidi Oct 05 '11 at 18:50
  • Hi @Chris, I changed the chmod to 777 one level up. I also used a absolutepath on the local machine instead of theabsolute path of the host. It works now! Thank you. – Laxmidi Oct 05 '11 at 21:26
  • Thankyou! In much need of this :D – cwiggo Nov 27 '14 at 12:03
  • 33
    CHMOD 777 is very unsecure and shouldn't be used – Black Jan 26 '16 at 11:51
  • @EdwardBlack What is a more secure alternative? Would "chmod -u www-data markers.xml" work? – Ayaskant Mishra Nov 16 '16 at 15:55
  • 2
    @AyaskantMishra you could use `chmod 760 markers.xml`. Owner has all permission, group has read and write permission and everyone else has no permission at all. – Black Nov 16 '16 at 19:59
-5

[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
Mark
  • 6,762
  • 1
  • 33
  • 50
  • 4
    This answer is complete nonsense. Instead of solving the problem you introduce a security hole. If you would have read the question thoroughly you would have seen that he wants to open a LOCAL file: fopen('markers.xml', 'w'); But you need allow_url_fopen=On only to open REMOTE files! For example for fopen('http://example.com/....xml'); Please see http://www.antary.de/2011/05/26/php-curl-als-alternative-fuer-allow_url_fopen – Elmue Dec 01 '14 at 15:48