首页 > 解决方案 > 如何在 CodeIgniter 控制器中读取 JSON 文件

问题描述

我无法在 codeigniter 控制器中读取我的 json 文件,它返回错误。

我的代码:

$curYear = date("Y");
$string = file_get_contents(base_url("assets/data/".$curYear."_cal.json"));

我的控制器尝试使用以下命令读取 json 文件返回错误消息:

file_get_contents(//localhost/EBR_Labeling/assets/data/2018_cal.json):无法打开流:没有这样的文件或目录


解决了

使用此语法:

file_get_contents("./assets/data/".$curYear."_cal.json")

标签: phpjsoncodeigniter

解决方案


请使用此代码-

<?php

$json = file_get_contents(FILE_PATH);
$obj  = json_decode($json);
echo '<pre>' . print_r($obj) . '</pre>';

?>

如果您需要任何其他帮助,请告诉我。

谢谢


推荐阅读