在现有文件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);
我正在尝试从yii2框架中的json文件中写入数据。 它返回错误无法打开流。 我的代码如下。 p>
$ productjson = json_encode($ value);
echo $ jsonfile = Yii :: $ app-> view-> theme-> baseUrl。'/ assets / json / aresult.json';
$ fp = fopen($ jsonfile,'w +');
nfwrite($ fp,$ productjson);
nclclose($ fp);
code> pre> \ n div>
答
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';