Reading files from the filesystem

Although you're probably going to be writing and reading data in a database you will certainly come in contact with the requirement to write something to the disk, and read from files stored on it. CodeIgniter can support several methods for interacting with files.

Getting ready

There are no configuration options to change here, but ensure that you load the file helper in your controller constructor (and also the url helper):

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

How to do it...

We're going to read files from the disk and display details about them to a view. Firstly, we're going to create two files:

  • /path/to/codeigniter/application/controllers/file.php ...

Get CodeIgniter 2 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.