【发布时间】:2015-02-09 22:17:42
【问题描述】:
我编写了一个 PHP 脚本,它获取 MySQL 查询的结果,对结果进行 json_encode,最后执行 file_put_contents() 操作。在我的 PC 上运行我的 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 似乎也无济于事。
【问题讨论】: