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
  • /path/to/codeigniter/application/views/file/view_file.php
  1. Add the following code into /path/to/codeigniter/application/controllers/file.php:
    <?php if (!defined('BASEPATH')) exit('No direct script access allowed'), 
    
    class File extends CI_Controller { 
        function __construct() { 
            parent::__construct(); 
            $this->load->helper('url'), 
            $this->load->helper('file'), 
        } 
    
        public function index() { 
            redirect('file/view_all_files'), 
        } 
    
        public function view_all_files() { 
            $data['dir'] = '/full/path/to/read';        
            $data['files'] = get_dir_file_info($data['dir']); 
            $this->load->view('files/view_file', $data); 
        } 
    } 
  2. Add the following code into /path/to/codeigniter/application/views/files/view_file.php:
    <html> 
        <head> 
            <title>Viewing Files</title> 
        </head> 
        <body> 
            <?php echo anchor('file/create_file', 'Create File'), ?> 
            <?php echo anchor('file/read_file', 'Read File'), ?> 
            <?php echo anchor('file/view_all_files', 'View Files'), ?> 
            <table border="1"> 
                <tr> 
                    <td><b>Filename</b></td> 
                    <td><b>Size</b></td> 
                    <td><b>Created</b></td> 
                    <td colspan="3">Actions</td> 
                </tr> 
                <?php foreach ($files as $file) : ?> 
                    <tr> 
                        <td> 
                            <?php if (is_dir($file['server_path'])) : ?> 
                                <b><?php echo $file['name']; ?></b> 
                            <?php else : ?> 
                                <?php echo $file['name']; ?> 
                            <?php endif; ?> 
                        </td> 
                        <td> 
                            <?php echo $file['size']; ?> 
                        </td> 
                        <td> 
                            <?php echo date("d/m/Y H:i:s", $file['date']); ?> 
                        </td> 
                        <td> 
                            <?php echo anchor('file/edit_file/' . $file['name'], 'Edit'), ?>&nbsp; 
                            <?php echo anchor('file/delete_file/' . $file['name'], 'Delete'), ?>&nbsp; 
                            <?php echo anchor('file/view_file/' . $file['name'], 'View'), ?> 
                        </td> 
                    </tr> 
                <?php endforeach; ?> 
            </table>
    </body> 
    </html> 

How it works...

The business end of this is the controller, function view_all_files(). We're doing three things. First, is setting the target directory with which we wish to read the line $data['dir'] = '/full/path/to/read'; obviously replacing '/full/path/to/read' with the actual path.

We then pass $data['dir'] to the CodeIgniter function, which does the heavy lifting for us, get_dir_file_info() returns an array for every item in the target directory. We store this in $data['files'].

$this->load->view('files/view_file', $data); calls the HTML template, passing to it the files array, which in turn loops through the $files array, outputting to an HTML table.

We also use the PHP function is_dir() to test whether an item is a directory or not; if it is, we make it bold in the HTML code—for no other reason than it's good to know what you're looking at.

It would be a great idea to move much of this functionality to a library or helper so that it can be more easily shared by other parts of your application if necessary.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.6.75