PHP fopen() 错误:无法打开流:权限被拒绝 [英] PHP fopen() Error: failed to open stream: Permission denied
问题描述
我正在学习如何编写 WordPress 插件.我需要一些帮助,将一些数据写入 XML 文件.我在我的本地机器上,一台运行 MAMP 的 Mac.我有 PHP 5.2.13.在我的插件中,我有:
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:
警告:fopen(markers.xml) [function.fopen]:无法打开流:权限被拒绝/Users/my_name/Sites/my_site/wp-content/plugins/my_plugin_folder/my_plugin_main_file.php在线 73
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
警告:fwrite():提供的参数不是有效的流资源/Users/my_name/Sites/my_site/wp-content/plugins/my_plugin_folder/my_plugin_main_file.php在线 75
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
警告:fclose():提供的参数不是有效的流资源/Users/my_name/Sites/my_site/wp-content/plugins/my_plugin_folder/my_plugin_main_file.php在线 76
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
我尝试在 $file_handle 行中使用绝对路径:http://my_site/wp-content/plugins/my_plugin_folder/markers.xml.但是,那没有用.
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.
我还尝试按如下方式更改 marker.xml 的权限:
I also tried changing the permissions on markers.xml as follows:
(我):阅读 &写(未知):只读大家:阅读&写
(Me): Read & Write (unknown): Read only everyone: Read & Write
出于某种原因,我的 Mac 不允许我将(未知)更改为 Read &写.我不确定这是否有所作为.我右键单击该文件并选择获取信息"以更改权限.
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.
在 phpInfo() 中,我有:
In phpInfo(), I've got:
注册 PHP 流 https、ftps、compress.zlib、compress.bzip2、php、文件、数据、http、ftp"
"Registered PHP Streams https, ftps, compress.zlib, compress.bzip2, php, file, data, http, ftp"
是否是 WordPress 设置导致了问题?还是只是 PHP 问题?
Is a WordPress setting causing the problem? or is it just PHP issue?
关于如何解决这个问题有什么建议吗?
Any suggestions on how to solve this problem?
谢谢.
推荐答案
您可能需要以管理员身份更改权限.在 Mac 上打开终端,然后打开markers.xml 所在的目录.然后输入:
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
系统可能会提示您输入密码.此外,它可能是不允许完全访问的目录.我不熟悉 WordPress,因此您可能需要更改每个向上移动到 mysite 目录的目录的权限.
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.
这篇关于PHP fopen() 错误:无法打开流:权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!