PHP fopen() Error: failed to open stream:

2019-01-18 10:03发布

问题:

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.

回答1:

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.



回答2:

[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