在现有文件Yii2框架中写入数据

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);

And yii2 has a class to work with json

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';