我编写了一个PHP脚本,它接受MySQL查询的结果,json_对结果进行编码,最后执行file_put_contents()操作。在我的电脑上运行Bitnami WAMP开发服务器时,脚本执行得很完美。然而,当在我的Mac上克隆我的git项目时(运行Yosemite)。在尝试执行file_put_contents()函数时,权限被拒绝。这是我的脚本:
<?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中运行unmask()和chmod(),但我都没有成功。即使在航站楼运行chmod或chown似乎也无济于事。
在PHP
脚本中运行echo exec('whoami');
,然后从浏览器中运行PHP脚本。它会告诉您谁是服务器应用程序的所有者。
然后chown
,该用户和组都可以,并根据您的意愿提供适当的权限。类似于:
chown -R user:user /my/folder
user
是从PHP脚本中发现的。