I've written a PHP script that takes the results of a MySQL query, json_encodes the results and finally does a file_put_contents() action. When running my Bitnami WAMP dev-server on my PC, the script executes perfectly. However, when clone my git project on my Mac (running Yosemite). I get permission denied upon trying to executing the file_put_contents() function. Here is my script:
<?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
This is the error I get:
[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
Here are the permissions in the directory I'm trying to write to:
drwxr-xr-x 15 myuser staff 510 Feb 6 19:13 restrict
The solutions I've heard are running an unmask() in PHP and a chmod() but I haven't had luck with either. Even running chmod or chown in the Terminal doesn't seem to help.
Run echo exec('whoami');
in a PHP
script and run the PHP script from browser. It gives you who is the owner of the server application.
Then chown
that user and group both and provide appropriate permissions as you wish. Something like:
chown -R user:user /my/folder
user
is discovered from the PHP script.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.