2015-07-06 59 views
-1

我想讀取CodeIgniter框架內的文件,它表示在控制器中,我的代碼在下面給出,但這不起作用,我在這裏有點新手,你們能指導我嗎如何讀取控制器內的文件?讀取CodeIgniter控制器中的文件

$fp = fopen("id_rsa.pem", "r"); 
$priv_key = fread($fp, 8192); 
fclose($fp); 
+0

不工作怎麼樣? – marekful

+0

你需要文件助手。 http://www.codeigniter.com/userguide3/helpers/file_helper.html – Craig

+0

id_rsa.pem位於控制器文件夾中?通過那裏的完整路徑 – Daimos

回答

2

如果設置BASE_URL在config.php 你可以在你的控制器加載這樣的文件

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... 
} 

例如用JSON文件使用谷歌API

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

希望幫助

2

你可以試試這個腳本..也加載助手庫

$this->load->helper('file'); 
$image_path = '/path/to/image/file'; 
$this->output->set_content_type(get_mime_by_extension($image_path)); 
$this->output->set_output(file_get_contents($image_path)); 
相關問題