Wednesday, 18 November 2015

Multiple image uploading using single input in CodeIgniter

CodeIgniter is a PHP framework, CodeIgniter has a number of helpers and libraries , which will reduce the development time and we can write more reliable and bugs free Code. Here ,The post is about uploading files in CodeIgniter, CodeIgniter has used upload library , by using this Class we can upload files on server easily.
For Uploading a files, as usually we need a Simple HTML from, with a input field and submit button.

In CodeIgniter views folder Create a new file called uploadform_view.php as below.
 
 
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo form_open_multipart('imageupload/doupload');?>
<input name="userfile[]" id="userfile" type="file" multiple="" />
<input type="submit" value="upload" />
<?php echo form_close() ?>
</body>
</html>
 
 

Create A New Controller file in Controller folder : controllers/imageupload.php
 
 
class Imageupload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('imageupload_view', array('error' => ' ' ));
}
function doupload() {
$name_array = array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key=>$value)
for($s=0; $s<=$count-1; $s++) {
$_FILES['userfile']['name']=$value['name'][$s];
$_FILES['userfile']['type']    = $value['type'][$s];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['userfile']['error']       = $value['error'][$s];
$_FILES['userfile']['size']    = $value['size'][$s];  
    $config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width']  = '1024';
$config['max_height']  = '768';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
$name_array[] = $data['file_name'];
}
$names= implode(',', $name_array);
/* $this->load->database();
$db_data = array('id'=> NULL,
'name'=> $names);
$this->db->insert('testtable',$db_data);
*/ print_r($names);
}
}
 
 

No comments:

Post a Comment

about new GraphQL freature in Magento2

GraphQL: GraphQL is a data query language developed internally by Facebook in 2012 before being publicly released in 2015. Magento impleme...