<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Multiupload extends MY_Controller {
/* -----------------------------------------------------------------------------------
* It's actualy not my own source, but I just want to share & bookmark it here ;)
*
* Thanks to Anggy Trisnawan
* Url: http://anggytrisnawan.com/blog/2012/07/21/upload-multiple-file-codeigniter
* -----------------------------------------------------------------------------------
*/
function __construct() {
parent::__construct();
// load the libraries at first
$this->load->library('image_lib');
$this->load->helper(array('form', 'url'));
}
// front view
function index() {
/* -----------------------------------------------------------------------------------
* It's what you need for the 'upload' view
* -----------------------------------------------------------------------------------
* <?php echo form_open_multipart('upload/do_upload');?>
*
* <!-- make sure you give an array (userfile[]) to the "name" attribute and have attribute "multiple" on it -->
* <?php echo form_input( array( 'name'=>'userfile[]', 'multiple'=>true ) ); ?><br />
*
* <!-- just, another submit button -->
* <?php echo form_submit('submit', 'Upload'); ?>
*
* <!-- well, close the form -->
* <?php echo form_close();?>
* -----------------------------------------------------------------------------------
*/
$data['multiupload'] = "multiupload"; // Controller
$data['view'] = "form_multiupload"; // View
$data['module'] = "multiupload"; // Controller
$data['error'] = ' ';
echo Modules::run('template/staff', $data);
}
// Upload & Resize in action
function do_upload() {
$upload_conf = array(
'upload_path' => realpath('assets/doc/img/real/'),
'allowed_types' => 'gif|jpg|png|JPEG|JPG',
'max_size' => '30000',
'encrypt_name' => TRUE,
'overwrite' => TRUE,
);
$this->load->library('upload', $upload_conf);
//$this->upload->initialize($upload_conf);
foreach ($_FILES['userfile'] as $key => $val)
{
$i = 1;
foreach ($val as $v)
{
$field_name = "file_" . $i;
$_FILES[$field_name][$key] = $v;
$i++;
}
}
// Unset the useless one ;)
unset($_FILES['userfile']);
foreach ($_FILES as $field_name => $file)
{
if (!$this->upload->do_upload($field_name))
{
echo $this->image_lib->display_errors();
}
else
{
$upload_data = $this->upload->data();
$ori = $upload_data['file_name'];
$mr = $upload_data['file_path'] . $ori;
$this->watermarking($mr);
//this is the larger image
$config['image_library'] = 'gd2';
$config['source_image'] = $upload_data['full_path'];
$config['new_image'] = 'assets/doc/img/full/' . $upload_data['file_name'];
$config['maintain_ratio'] = TRUE;
$config['width'] = 0;
$config['height'] = 0;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
//this is the larger image
$config['image_library'] = 'gd2';
$config['source_image'] = $upload_data['full_path'];
$config['new_image'] = 'assets/doc/img/medium/' . $upload_data['file_name'];
$config['maintain_ratio'] = TRUE;
$config['width'] = 342;
$config['height'] = 342;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
//small image
$config['image_library'] = 'gd2';
$config['source_image'] = $upload_data['full_path'];
$config['new_image'] = 'assets/doc/img/small/' . $upload_data['file_name'];
$config['maintain_ratio'] = TRUE;
$config['width'] = 152;
$config['height'] = 152;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
//cropped thumbnail
$config['image_library'] = 'gd2';
$config['source_image'] = $upload_data['full_path'];
$config['new_image'] = 'assets/doc/img/thumbnails/' . $upload_data['file_name'];
$config['maintain_ratio'] = TRUE;
$config['width'] = 62;
$config['height'] = 62;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
// do it!
if (!$this->image_lib->resize()) {
// if got fail.
//$error['resize'][] = $this->image_lib->display_errors();
echo $this->image_lib->display_errors();
} else {
// otherwise, put each upload data to an array.
$success[] = $upload_data;
}
}
}
// see what we get
if (count($error > 0)) {
$data['error'] = $error;
} else {
$data['success'] = $upload_data;
}
$data['multiupload'] = "multiupload"; // Controller
$data['view'] = "index_multiupload"; // View
$data['module'] = "multiupload"; // Controller
$data['error'] = ' ';
dump($this->upload->data());
echo Modules::run('template/staff', $data);
}
}
/* End of File: multiupload.php */
- Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- No direct script access allowed
preferences:
347.02 ms | 406 KiB | 460 Q