Created
March 6, 2014 19:18
-
-
Save iammilton82/9397319 to your computer and use it in GitHub Desktop.
MultiFile Upload function in CodeIgniter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- CONTROLLER --> | |
<? if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class Homepage extends CI_Controller { | |
function Homepage(){ | |
parent::__construct(); | |
} | |
public function upload() | |
{ | |
$info = array(); | |
$uploaded = array(); | |
$content = array(); | |
$state = 'pre'; | |
if ($this->input->server('REQUEST_METHOD') == 'POST'){ | |
$state = 'post'; | |
$files = $_FILES; | |
$settings = array(); | |
$settings['field'] = 'photo'; | |
$settings['folder'] = 'assets/uploads/temp'; | |
$settings['makeThumb'] = true; | |
$settings['allowed'] = "gif|jpg|jpeg|png"; | |
$numFileUploads = count($_FILES[$settings['field']]['name']); | |
if($numFileUploads >= 1){ | |
$uploaded = $this->upload->uploadmultiple($files, $settings); | |
if($uploaded) | |
{ | |
$info = $uploaded; | |
} else { | |
$info = false; | |
} | |
} else { | |
$info = false; | |
$state = 'error'; | |
} | |
} | |
$content['title'] = "Upload Photo"; | |
$content['array'] = $info; | |
$content['state'] = $state; | |
$header['title'] = "Upload Photo"; | |
$this->load->view('template/header', $header); | |
$this->load->view('template/upload', $content); | |
$this->load->view('template/footer'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- LIBRARY --> | |
<? | |
if (!defined('BASEPATH')) exit('No direct script access allowed'); | |
class MY_Upload extends CI_Upload { | |
function uploadmultiple($files, $settings) | |
{ | |
$uploaded = array(); | |
$field = $settings['field']; | |
$makeThumb = $settings['makeThumb']; | |
$folder = $settings['folder']; | |
$allowed = $settings['allowed']; | |
$numFileUploads = count($_FILES[$field]['name']); | |
if($numFileUploads == 0) | |
{ | |
$uploaded['message'] = 'nothing to upload'; | |
} else { | |
for($i=0; $i<$numFileUploads; $i++) | |
{ | |
$_FILES[$field]['name']= $files[$field]['name'][$i]; | |
$_FILES[$field]['type']= $files[$field]['type'][$i]; | |
$_FILES[$field]['tmp_name']= $files[$field]['tmp_name'][$i]; | |
$_FILES[$field]['error']= $files[$field]['error'][$i]; | |
$_FILES[$field]['size']= $files[$field]['size'][$i]; | |
$config['upload_path'] = FCPATH.$folder; | |
$config['remove_spaces'] = TRUE; | |
$config['overwrite'] = TRUE; | |
$config['max_size'] = 15000; | |
$config['allowed_types'] = $allowed; | |
$this->initialize($config); | |
$upload = $this->do_upload($field); | |
$data = $this->data(); | |
if(!$upload) | |
{ | |
$filename = $_FILES['photo']['name']; | |
} else { | |
$filename = $_FILES['photo']['name']; | |
$uploaded[$i] = $data; | |
if($makeThumb == true) | |
{ | |
$thumbSettings['width'] = 250; | |
$thumbSettings['height'] = 250; | |
$thumbSettings['filename'] = $filename; | |
$thumbSettings['source'] = $folder."/".$filename; | |
$thumbSettings['destination'] = $folder."/thumb_".$filename; | |
$thumb = $this->createthumbnail($thumbSettings); | |
# $uploaded[$i]['thumbnail'] = $file['raw_name']."_thumb".$file['file_ext']; | |
} | |
} | |
} | |
} | |
return $uploaded; | |
} | |
function createthumbnail($settings) | |
{ | |
$ci = &get_instance(); | |
$ci->load->library('image_lib'); | |
$source = $settings['source']; | |
$destination = $settings['destination']; | |
$width = $settings['width']; | |
$height = $settings['height']; | |
$config['image_library'] = "gd2"; | |
$config['source_image'] = FCPATH."/".$source; | |
$config['new_image'] = FCPATH."/".$destination; | |
$config['create_thumb'] = TRUE; | |
$config['maintain_ratio'] = TRUE; | |
$config['width'] = $width; | |
$config['height'] = $height; | |
$ci->image_lib->initialize($config); | |
if(!$ci->image_lib->resize()) | |
{ | |
return false; | |
} else { | |
return true; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- VIEW --> | |
<div class="container-light-blue"> | |
<div id="content"> | |
<div class="container"> | |
<div class="row"> | |
<header class="col-6 col-lg-6 col-offset-3"> | |
<h1 class="page-title"><?=$title?></h1> | |
</header> | |
</div> | |
<div class="row"> | |
<div class="col-6 col-lg-6 col-offset-3"> | |
<? | |
switch($state): | |
case 'error': | |
echo "<div class='label label-danger'>There was an error uploading your file</div>"; | |
break; | |
case 'post': | |
if(!empty($array)) | |
{ | |
echo "<pre>"; | |
print_r($array); | |
echo '</pre>'; | |
} else { | |
echo "<div class='label label-danger'>There was an error uploading your file</div>"; | |
} | |
break; | |
default: | |
echo "<p>Use the form below to upload files</p>"; | |
break; | |
endswitch; | |
echo form_open_multipart(current_url()); | |
echo $this->forms->fileUploadMulti("photo", "Choose photo "); | |
echo $this->forms->submit("Upload Photo(s)", "btn btn-success btn-lg"); | |
echo form_close(); | |
?> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment