I am trying to write data in a json file from yii2 framework. It's returning error failed to open stream. My code is given below.
$productjson = json_encode($value);
echo $jsonfile=Yii::$app->view->theme->baseUrl.'/assets/json/aresult.json';
$fp = fopen($jsonfile, 'w+');
fwrite($fp, $productjson);
fclose($fp);
That's the right way to specify the path
$productjson = json_encode($value);
echo $jsonfile= Yii::getAlias('@webroot/assets/aresult.json');
$fp = fopen($jsonfile, 'w+');
fwrite($fp, $productjson);
fclose($fp);
Your $jsonfile
variable contains the file URL, while it should contain your file's path in the server. Check the predefined aliases.
For example:
$jsonfile=Yii::getAlias('@app').'/assets/json/aresult.json';