Load external library in codeigniter

14.6k Views Asked by At

How can I access the getid3 class and use it in my controller?

project
 /application
 /controller
 /libraries
    /getID3
       /getid3
          getid3.php
 /model
 /views   
4

There are 4 best solutions below

0
Bhavin On

Use CodeIgniter's built in constant, APPPATH. (Because code is not written in codeigniter's syntaxes)

`require_once(APPPATH.'libraries/getID3/getid3/getid3.php');`

If this library is codeigniter's built in library then you should use.

$this->load->library('libary name');
1
Regolith On

From CI Documentation

you can load library by doing

$this->load->library('getID3/getid3/Getid3');

As explained in documentation if you have file located in a subdirectory like
libraries/flavors/Chocolate.php
You will load it using:

$this->load->library('flavors/chocolate');

0
KFE On

You COULD just do a require_once on the main GetID3.php file

require_once(APPPATH.'libraries/getID3/getid3/getid3.php');

but there are better, more cleaner ways, to manage this third party dependency.

Instead of directly requiring the GetID3.php file, I recommend creating a wrapper class for the library. Creating a wrapper class affords you the ability to extend/overwrite the getid3 library and provides a cleaner implementation by allowing you to do things the "codeigniter way".

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class GetID3 {  

    function __construct ( $options = array() )
    {
        require_once( APPPATH . 'third_party/getID3/getid3/getid3.php' );
    }

    public function __get( $var ) { return get_instance()->$var; }
}

Doing it this way provides a cleaner interface to work with and allows for a more scalable approach to managing the third party dependency. Also, not the directory structure. Here, we are saving third party dependencies to the third_party directory while saving the wrapper class to libraries/GetID3.php.

Once you've implemented it this way, you can load the library as you normally would:

$this->load->library('GetID3');
0
always-a-learner On

Regarding Your File Structure Codeigniter has the solution for it:

project
/application
/controller
/libraries
    /getID3
        /getid3
            getid3.php
/model
/views 

Now To call the Getid3.php the library you need to add this below code in the controller.

$this->load->library ( 'getID3/getid3/getid3', '', 'getid3(you can add any name you want' );

Now to Use this:

$this->getid3->your_function($data);

Please Note that Getid3.php must start with the capital letter.