Want to have an Online Store but don't know where to start?
Click here and start selling online!

Creating your own library in CodeIgniter

February 3, 2008 | In: CodeIgniter, PHP, library

You setup CodeIgniter and you see how it really helps your life as a developer doing you job. But you want your codes to be reusable into your other projects. Yes you can say i’ll just copy-paste the codes, but there’s more elegant idea, build it as a library. What is a library, IMO, a library is a file or a component that can be easily inject into an existing system.

Nuf said, here’s the code:

<?php
/**
* class for easy uploading of files for CI
* @author zeratool
* @uses CI::Upload class
*
*/
class lib_upload
{
        function __construct()
        {
             $config['upload_path'] = '/your/path/to/upload/dir/';
             $config['allowed_types'] = 'gif|jpg|png';
             $this->CI =& get_instance();
             $this->CI->load->library('upload', $config);
        }
 
        /**
         * uploads the file
         * @access public
         *
         */
         function do_upload()
         {
              $data = $this->validate();
              return $data;
         }
 
        /**
         * checks the file uploaded
         * @access private
         * @return mixed, string if error, array of file uploaded info on success
         */
          private function validate()
          {
              if (! $this->CI->upload->do_upload())
              {
                  return $this->CI->upload->display_errors();
              }
             else
             {
                  return $this->CI->upload->data();
             }
       }
 
}
 
?>

Now in order to use our library, we have to create our Controller and View files.

First, the controller:

<?php
class upload extends Controller
{
       function __construct()
       {
            parent::Controller();
            //we use our custom library by loading it first
            $this->load->library('lib_upload');
       }
 
       function index()
       {
           // we load our view file here, in our case it's the upload form
           $this->load->view('uploadform');
       }
 
       function do_upload()
       {
            // we call the do_upload method of our library
            $ok = $this->lib_upload->do_upload();
 
            // we checked if the upload is successful or not
            // if it displays the array of the file information, try to check your
            // upload directory and look for the file there
            // NOTE : you must set the upload dir enough write priveledges
            echo '<pre>';
            print_r($ok);          
 
      }
}
?>

Here’s our View file

<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?=$error;?>
 
<?=form_open_multipart('upload/file/do_upload');?>
 
<input type="file" name="userfile" size="20" / ?>
 
<br>
 
<input type="submit" value="upload" / ?>
 
</form>
 
</body></html>
Share on Facebook


4 Responses to Creating your own library in CodeIgniter

Avatar

jonatan

July 30th, 2008 at 6:12 am

Hi!
Can you tell us how to use the class.upload (http://www.verot.net/php_class_upload.htm) in CI?

Thanks

Avatar

admin

August 3rd, 2008 at 3:05 am

Hi Jonathan,

You have to create a wrapper library that will handle/call the class.upload.php file. Then you will use that wrapper inside your controller.


< ?php

// application/libraries/Wrapper.php

require 'class.upload.php';

class Wrapper {

function process($files_arr) {

$handle = new upload($files_arr); //$_FILES['image_field']

if ($handle->uploaded) {

$handle->file_new_name_body = 'image_resized'; // the new name of the uploaded file
$handle->image_resize = true;
$handle->image_x = 150; // copied image width
$handle->image_ratio_y = true;
$handle->process('/my/upload/dir/'); // upload destination, make sure it's writable by webserver

if ($handle->processed) {
echo 'image resized';
$handle->clean();

} else {

echo 'error : ' . $handle->error;

}
}

}

}

Then in your controller , you will use it like this:


< ?php
// application/controllers/testwrapper.php

class testwrapper extends Controller {

function __construct() {

parent::__construct();
$this->load->library('Wrapper');
}

function index() {

$this->load->view("upload_form"); // view/template for file upload
}

function upload() {

$this->wrapper->process($_FILES['file']); // files array
}
}

Now, on your view file :
// application/views/upload_form.php


<form method="POST" action="<?=site_url('testwrapper/upload');?>" enctype="multipart/form-data">

File : <input type="file" name="file">
<input type="submit" name="uploadfile" value="Upload File">
</form>

Hope this helps and thanks for the comment.

Avatar

endra

February 24th, 2009 at 11:30 pm

hi..
nice lib..
i never do that for my project..
coz i’m still newbie..
hahahaha..
i want to ask u something..
is it possible to upload php file using codeigniter class upload..??
i have problem with that..

Avatar

admin

February 25th, 2009 at 7:17 am

@endra

Yes it is! You can use directly without wrapping it inside a library class. I just use it merely for example purpose.

Hope this helps.

Thanks for the comment! :)

Comment Form

Advertisement

Get Updates

Subscribe
free counters