Creating your own library in CodeIgniter
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;
}
/**
* cehcks 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 /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>

February 02, 2008





Hi!
Can you tell us how to use the class.upload (http://www.verot.net/php_class_upload.htm) in CI?
Thanks
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.