CodeIgniter - 写入json文件

时间:2016-07-20 14:02:07

标签: php mysql json codeigniter

我已设法打印出来屏幕我的JSON文件,但我无法写入该文件。任何人都可以提供帮助,我是PHP / codeIgniter的新手,但努力学习。我的代码如下。

模型类

<?php
class DBModel extends CI_Model {

        function getData() {
          $query = $this->db->get('profile');
          return json_encode($query->result());
    }  // end function
}  // end class
?>

控制器类

<?php
class Json extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('DBModel');
        $this->load->helper('file');
    }

    public function index() {
        $this->load->model('DBModel');
        $data = $this->DBModel->getData();

      // echo json_encode($data);

         $fp = fopen('./artist_file.json', 'w');
         fwrite($fp, json_encode($data));

         if ( ! write_file('./artist_file.json', $arr))
         {
             echo 'Unable to write the file';
         }
         else
         {
             echo 'file written';
         }
         $this->load->view('jsonView', $data);
    }
}  // end of class
?>

这些是我收到的错误消息

Filename: controllers/Json.php

Line Number: 19

Backtrace:

File: /Applications/XAMPP/xamppfiles/htdocs/Artists/application/controllers/Json.php
Line: 19
Function: fopen

File: /Applications/XAMPP/xamppfiles/htdocs/artists/index.php
Line: 315
Function: require_once
___________________________________________

A PHP Error was encountered

Severity: Warning

Message: fwrite() expects parameter 1 to be resource, boolean given

Filename: controllers/Json.php

Line Number: 20

Backtrace:

File: /Applications/XAMPP/xamppfiles/htdocs/Artists/application/controllers/Json.php
Line: 20
Function: fwrite

File: /Applications/XAMPP/xamppfiles/htdocs/artists/index.php
Line: 315
Function: require_once
__________________________________________

A PHP Error was encountered

Severity: Warning

Message: Missing argument 2 for write_file(), called in /Applications/XAMPP/xamppfiles/htdocs/Artists/application/controllers/Json.php on line 22 and defined

Filename: helpers/file_helper.php

Line Number: 85

Backtrace:

File: /Applications/XAMPP/xamppfiles/htdocs/Artists/application/controllers/Json.php
Line: 22
Function: write_file

File: /Applications/XAMPP/xamppfiles/htdocs/artists/index.php
Line: 315
Function: require_once

0 个答案:

没有答案