在CodeIgniter中的视图文件中写入数据

I am trying to write some JSON data in a view file in CodeIgniter.

But it shows an error:

Message: fopen() [function.fopen]: Filename cannot be empty

The controller function is as follows:

public function write_in_a_view_file() {

    $data = array();
    $data['title'] = "Write This Title In a View File";

    $view_file = $this->load->view('result.php');

    $fp = fopen($view_file, 'w');
    fwrite($fp, json_encode($data));
    fclose($fp);

}

You can use the CodeIgniter File Helper for this.

public function write_in_a_view_file() {
    $data = array();
    $data['title'] = "Write This Title In a View File";

    $this->load->helper('file');
    $new_file_path = base_url() . '/path/to/file.php'; // modify this line to point to the actual location of the file
    write_file($new_file_path, json_encode($data));
}

fopen function takes 2 arguments, the first one must be the file name, while you are passing nothing to the first param.