Mac OS X上的文件夹和文件权限问题
问题描述:
我编写了一个PHP脚本,它接受MySQL查询的结果,json_encodes结果并最终执行file_put_contents()操作。当在我的PC上运行我的Bitnami WAMP dev-server时,脚本执行完美。但是,当我的Mac(运行优胜美地)克隆我的git项目。尝试执行file_put_contents()函数时,我获得权限被拒绝。这里是我的脚本:Mac OS X上的文件夹和文件权限问题
<?php
// All Articles to JSON //
if(array_key_exists("makejson", $_REQUEST)) {
// Class to Serialize The JSON
class ArrayValue implements JsonSerializable {
public function __construct(array $array) {
$this->array = $array;
}
public function jsonSerialize() {
return $this->array;
}
}
// Designate the file
$file = "articles.json";
// FHA articles query
$milArticles = $dataConnection->SelectColsWhere("text_news", "active='1' ORDER BY ndate DESC", "textnewsid,ndate,ntitle,sentence");
if(count($milArticles) > 0){
$json_data = json_encode(new ArrayValue($milArticles), JSON_HEX_APOS | JSON_PRETTY_PRINT);
// Check for JSON errors
if ($json_data === null && json_last_error() !== JSON_ERROR_NONE) {
throw new \LogicException(sprintf("Failed to parse json string '%s', error: '%s'", $json_data , json_last_error_msg()));
} else {
file_put_contents($file, $json_data, LOCK_EX);
}
}
}// END OF MAKE JSON
这是错误我得到:
[Mon Feb 09 16:06:20.522798 2015] [:error] [pid 686] [client 127.0.0.1:50195] PHP Warning: file_put_contents(articles.json): failed to open stream: Permission denied in /Users/myuser/Sites/PIXEL/militaryinfo/restrict/includes/articleJson.php on line 33, referer: http://militaryinfo/restrict/submit_JSON.php
这里有我想要写目录的权限:
drwxr-xr-x 15 myuser staff 510 Feb 6 19:13 restrict
的解决方案我听说在PHP和chmod()中运行unmask(),但我没有运气。即使在终端中运行chmod或chown似乎也没有帮助。
答
在PHP
脚本中运行echo exec('whoami');
并从浏览器运行PHP脚本。它给你谁是服务器应用程序的所有者。
然后chown
该用户和组都提供适当的权限,如您所愿。例如:
chown -R user:user /my/folder
user
是从PHP脚本中发现的。
它看起来像安装了Mac OS X的Apache,所有文件/文件夹的所有者是_www。所以我只需要将用户更改为“myuser”。像:chown -R myuser:用户/我的/文件夹? – 2015-02-09 22:40:53
'chown -R _www:_www/my/folder'。然后,你可以做一个'chmod -R 755/my/folder'。如果需要,使用'sudo'。 – activatedgeek 2015-02-09 22:45:47
我现在得到这个错误:[Mon Feb 09 17:02:51.339966 2015] [core:notice] [pid 1095] AH00094:命令行:'/ usr/sbin/httpd -D FOREGROUND' – 2015-02-09 23:07:52