I have been trying to upload multiple photos twice in a single form in codeigniter project,though the photos are uploading correctly but the name of photos are not been returned as per desired. for example if am uploading a photo named 'a.jpg' in first uploader and 'b.jpg' in second uploader,the names i am getting are "a.jpg,b.jpg" for first and "b.jpg,b(1).jpg" for second case. below is the code attached.
controller:-
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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']);
// echo $count;
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'] = 'application/views/uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000000';
//$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);
return $names;
}
function doupload_1()
{
$name_array = array();
$count = count($_FILES['file_1']['size']);
echo $count;
foreach($_FILES as $key=>$value)
for($s=0; $s<=$count-1; $s++)
{
$_FILES['file_1']['name']=$value['name'][$s];
$_FILES['file_1']['type'] = $value['type'][$s];
$_FILES['file_1']['error'] = $value['error'][$s];
$_FILES['file_1']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['file_1']['size'] = $value['size'][$s];
$config['upload_path'] = 'application/views/uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000000';
//$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);
return $names;
}
public function check()
{
$data = $this->doupload();
$data_1 = $this->doupload_1();
print_r($data);
echo "<br>";
print_r($data_1);
}
}
?>
view:-
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo form_open_multipart('imageupload/check');?>
<input name="userfile[]" id="userfile" type="file" multiple="" />
<input name="file_1[]" id="file" type="file" multiple=""/>
<input type="submit" value="upload" />
<?php echo form_close() ?>
</body>
</html>
thanks in advance!
Finally i got what the problem in your code. it is because your foreach on $_FILES not on $_FILES['userfile'] thats why $_FILES CONTAIN all the files not only userfile. and file_1
foreach($_FILES['userfile'] as $key=>$value)
foreach($_FILES['file_1'] as $key=>$value)
but still you have to change the below code according to foreach. if you find solution for your answer please givel like my answer and accept it .
Please check below link to upload multi file in codeigniter
http://technet.massivetechnolab.co.in/multiple-image-uploading-using-codeigniter
Multi upload function
public function mupload ($field_name,$path = './botimage/') {
$files = $_FILES;
$cpt = count($_FILES[$field_name]['name']);
for ($i = 0; $i < $cpt; $i++) {
$config = array(
'upload_path' => $path,
'upload_url' => upload_url('galeri/'),
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'file_name' => rand(0,999). time(),
'max_size' => "10000KB",
);
$data = array();
// set the filter image types
$this->load->library('upload', $config);
$_FILES['userfile']['name'] = $files[$field_name]['name'][$i];
$_FILES['userfile']['type'] = $files[$field_name]['type'][$i];
$_FILES['userfile']['tmp_name'] = $files[$field_name]['tmp_name'][$i];
$_FILES['userfile']['error'] = $files[$field_name]['error'][$i];
$_FILES['userfile']['size'] = $files[$field_name]['size'][$i];
$this->upload->initialize($config);
if ($this->upload->do_upload()) {
$tmp = $this->upload->data();
$uploaded_file_data[$i] = $tmp["file_name"];
} else {
$errors[] = $this->upload->display_errors();
}
}
$data["file_urls"] = implode(",",$uploaded_file_data);
$data["errors"] = implode(" ",@$errors);
return $data;
}