我正在尝试在CodeIgniter框架中读取文件,这意味着在控制器中,下面给出了我的代码,但这不起作用,我在这里有点新手,你们可以指导我如何在控制器内部读取文件吗?
$fp = fopen("id_rsa.pem", "r");
$priv_key = fread($fp, 8192);
fclose($fp);
如果您在config.php中设置了base_url,则可以在控制器中加载这样的文件
public function load_file()
{
// you need to load the url helper to call base_url()
$this->load->helper("url");
// you can change the location of your file wherever you want
$MyFile = file_get_contents(base_url()."application/controllers/readme.txt");
var_dump($MyFile);
//etc...
}
使用google api的json文件示例
public function load_file_via_api()
{
$json = file_get_contents('https://www.googleapis.com/youtube/v3/videos?key=AIzaSyBdiYy5i2sI5HSkLYy54xoYIXn0M8OgGoA&id=DniEWhn7fvA&part=snippet&fields=items(snippet(title))');
$obj = json_decode($json);
print '<pre>' . print_r($obj) . '</pre>';
}
在此处阅读有关file_get_contents的更多信息http://php.net/manual/en/function.file-get-contents.php
希望能有所帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句