macos php fopen 无法打开用于在 Mac OS X 中写入的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5846655/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:01:03  来源:igfitidea点击:

php fopen fails to open a file for writing in Mac OS X

phpapachemacosfile-permissionsfopen

提问by Arman

I'm running php script updreading.php locally on my laptop (with Mac OS X 10.6.7). Here's the contents of updreading.php

我在我的笔记本电脑上本地运行 php 脚本 updreading.php(使用 Mac OS X 10.6.7)。下面是updreading.php的内容

<?php
    $outFile = "examples-output.txt";
    $out = fopen($outFile, 'w') or die("can't open write file");
?>

When I run it, I get "can't open write file" error message. I think it should have something to do with permissions. I've made myself (arman) an owner of /Library/WebServer/Documents/ with Read&Write permissions and 'applied settings to enclosed items'. My script and file I'm writing to (examples-output.txt) is located in /Library/WebServer/Documents/wabun/. Here are the permissions for files in /Library/WebServer/Documents/wabun/:

当我运行它时,我收到“无法打开写入文件”的错误消息。我认为它应该与权限有关。我已经让自己 (arman) 成为 /Library/WebServer/Documents/ 的所有者,具有读写权限和“将设置应用于封闭项目”。我正在写入的脚本和文件 (examples-output.txt) 位于 /Library/WebServer/Documents/wabun/。以下是 /Library/WebServer/Documents/wabun/ 中文件的权限:

-rw-r--r--@ 1 arman  admin         0 May  1 01:03 examples-output.txt
-rw-r--r--@ 1 arman  staff      1657 May  1 01:04 updreading.php

I was trying to resolve the issue for the last 4 hours trying different permissions without luck. Any ideas how to resolve this issue, guys? Thanks!

在过去的 4 个小时里,我试图解决这个问题,但没有运气尝试不同的权限。任何想法如何解决这个问题,伙计们?谢谢!

采纳答案by Alix Axel

I bet the issue is that your webserver or PHP instance is not running under the same user (arman), you can check this using the get_current_user()function. You can either configure apache to run with your privileges (ask in ServerFault) or give the folder more generous permissions, like 0777.

我敢打赌,问题是您的网络服务器或 PHP 实例未在同一用户 ( arman)下运行,您可以使用该get_current_user()函数进行检查。您可以将 apache 配置为以您的权限运行(在 ServerFault 中询问)或为文件夹提供更慷慨的权限,例如0777.

Another thing, are you sure $outFilemaps to an absolutepath? Try doing:

另一件事,你确定$outFile映射到绝对路径吗?尝试做:

var_dump(is_file($outfile));

回答by Chris

I was having this exact same problem. I had to make sure the permissions were correct for all of the parentfolders as well. For instance, if (in OSX) trying to write to the directory ~/foo/bar/baz, I solved it with the command chmod -R 777 ~/foo.

我遇到了完全相同的问题。我还必须确保所有文件夹的权限都正确。例如,如果(在 OSX 中)尝试写入目录 ~/foo/bar/baz,我使用命令解决了它chmod -R 777 ~/foo

回答by Crayon Violent

you need to make apache (assuming apache is server you are using) the owner or put in group (and give group write access) or give write access to all or etc.. IOW apache is the one actually executing the script, not you.

您需要使 apache(假设 apache 是您正在使用的服务器)成为所有者或放入组中(并授予组写访问权限)或授予所有或其他人的写访问权限。IOW apache 是实际执行脚本的人,而不是您。