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.

4

2 回答 2

35

您可能需要以管理员身份更改权限。在 Mac 上打开终端,然后打开 markers.xml 所在的目录。然后键入:

sudo chmod 777 markers.xml

系统可能会提示您输入密码。此外,它可能是不允许完全访问的目录。我不熟悉 WordPress,因此您可能必须更改每个目录的权限,向上移动到 mysite 目录。

于 2011-10-05T17:25:10.753 回答
-5

[function.fopen]: failed to open stream

如果您有权访问 php.ini 文件,请尝试启用 Fopen。找到相应的行并将其设置为“on”:& if 在 wp 中,例如 php.ini 中的 localhost/wordpress/function.fopen :

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
于 2013-06-19T22:55:46.200 回答