PHP fopen() 错误:无法打开流:权限被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7665093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP fopen() Error: failed to open stream: Permission denied
提问by Laxmidi
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:
我正在学习如何编写 WordPress 插件。我需要一些帮助,将一些数据写入 XML 文件。我在我的本地机器上,一台运行 MAMP 的 Mac。我有 PHP 5.2.13。在我的插件中,我有:
$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
警告:fopen(markers.xml) [function.fopen]:无法打开流:第 73 行的 /Users/my_name/Sites/my_site/wp-content/plugins/my_plugin_folder/my_plugin_main_file.php 中的权限被拒绝
警告:fwrite():在第 75 行的 /Users/my_name/Sites/my_site/wp-content/plugins/my_plugin_folder/my_plugin_main_file.php 中提供的参数不是有效的流资源
警告:fclose():在第 76 行的 /Users/my_name/Sites/my_site/wp-content/plugins/my_plugin_folder/my_plugin_main_file.php 中提供的参数不是有效的流资源
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.
我尝试在 $file_handle 行中使用绝对路径:http://my_site/wp-content/plugins/my_plugin_folder/markers.xml。但是,那没有用。
I also tried changing the permissions on markers.xml as follows:
我还尝试更改markers.xml 的权限,如下所示:
(Me): Read & Write (unknown): Read only everyone: Read & Write
(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.
出于某种原因,我的 Mac 不允许我将(未知)更改为读写。我不确定这是否有所作为。我右键单击该文件并选择“获取信息”以更改权限。
In phpInfo(), I've got:
在 phpInfo() 中,我有:
"Registered PHP Streams https, ftps, compress.zlib, compress.bzip2, php, file, data, http, ftp"
“注册 PHP 流 https、ftps、compress.zlib、compress.bzip2、php、文件、数据、http、ftp”
Is a WordPress setting causing the problem? or is it just PHP issue?
是 WordPress 设置导致了问题吗?还是只是 PHP 问题?
Any suggestions on how to solve this problem?
有关如何解决此问题的任何建议?
Thank you.
谢谢你。
回答by Chris
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:
您可能需要以管理员身份更改权限。在 Mac 上打开终端,然后打开markers.xml 所在的目录。然后键入:
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.
可能会提示您输入密码。此外,它可能是不允许完全访问的目录。我对 WordPress 不熟悉,因此您可能需要更改每个向上移动到 mysite 目录的目录的权限。
回答by Shaun Palmer
[function.fopen]: failed to open stream
[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 :
如果您有权访问 php.ini 文件,请尝试启用 Fopen。找到相应的行并将其设置为“on”:&如果在 wp 中,例如 localhost/wordpress/function.fopen 在 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