CodeIgniter free upload helper
by krike in Freebees / PHP classes on 18 May 2010
by krike in Freebees / PHP classes on 18 May 2010
For one of my projects I had several pages where people could upload images or other files. Now knowing that OOP has as purpose to limit the code and make the same back-end code available for different functionalities. I created an upload helper file for CodeIgniter. This helper file should allow you to create your [...]
For one of my projects I had several pages where people could upload images or other files. Now knowing that OOP has as purpose to limit the code and make the same back-end code available for different functionalities. I created an upload helper file for CodeIgniter. This helper file should allow you to create your upload forms (well the back-end) much faster.
Save this code into a file named upload_helper.php in the helpers folder and don’t forget to load the file when you use the helper.
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/*###################################################################################*/
function multiple_upload($name = 'userfile', $upload_dir = 'sources/images/', $allowed_types = 'gif|jpg|jpeg|jpe|png', $size)
/*###################################################################################*/
{
$CI =& get_instance();
$config['upload_path'] = realpath($upload_dir);
$config['allowed_types'] = $allowed_types;
$config['max_size'] = $size;
$config['overwrite'] = FALSE;
$config['encrypt_name'] = TRUE;
$CI->upload->initialize($config);
$errors = FALSE;
if(!$CI->upload->do_upload($name)):
$errors = TRUE;
else:
// Build a file array from all uploaded files
$files = $CI->upload->data();
endif;
// There was errors, we have to delete the uploaded files
if($errors):
@unlink($files['full_path']);
return false;
else:
return $files;
endif;
}//end of multiple_upload()
/*#################################################*/
//modified version of jeffrey way's zip extract tutorial
function upload_batch_images($name = 'userfile', $upload_dir = 'sources/images/', $allowed_types = 'gif|jpg|jpeg|jpe|png', $size)
/*#################################################*/
{
$CI =& get_instance();
$realpath = $upload_dir."images-".rand(1111111111,9999999999); //let's make it unique
$config['upload_path'] = $realpath;
$config['allowed_types'] = $allowed_types;
$config['max_size'] = $size;
$config['overwrite'] = FALSE;
$config['encrypt_name'] = TRUE;
$CI->upload->initialize($config);
if(mkdir($realpath)):
if($CI->upload->do_upload($name)):
$files = $CI->upload->data();
if(openZip($realpath."/".$files['file_name'], $realpath)):
@unlink($files['full_path']);
return $realpath;
else:
@unlink($files['full_path']);
return false;
endif;
endif;
else:
return false;
endif;
}
/*#################################################*/
function openZip($file_to_open, $zip_target)
/*#################################################*/
{
$zip = new ZipArchive();
$x = $zip->open($file_to_open);
if ($x === true):
$zip->extractTo($zip_target);
$zip->close();
return true;
else:
return false;
endif;
}
?>
Controller example code
//returns an array and store it into an variable
//requires 4 parameters, the name of the file input, the path where the file should be uploaded too, the allowed extentions, the max. file size
$s_thumb = multiple_upload('small_thumb', 'sources/images/', 'gif|jpg|jpeg|jpe|png', 1024);
echo $s_thumb->file_name; //display file name
/*
returns the following array, which is the basic array when a file is uploaded in CodeIgniter
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
*/
//the following will upload a zip file and extract all the images or files within it, it returns only the path of the image folder
$batch_images = upload_batch_images('large_thumb', 'sources/images/', 'zip', 30720000);
If you like this upload helper share it with other people.
4 Comments
Thank you, this code was very usefull for me.
@xterico: you are welcome
googe helper, u save me =)
@Rodrigo: you are welcome
Drop us a word