Reputation: 735
While uploading the multiple files in codeigniter i am getting error like this,
Fatal error: Unsupported operand types in .../system\libraries\Upload.php
if(isset($_FILES['med_file']))
{
$config['upload_path'] = './medical_history_doc/';
$config['allowed_types'] = 'jpg|jpeg|png|doc|docx|pdf|txt';
$this->load->library('upload', $config);
$files = $_FILES;
$cpt = count($_FILES['med_file']['name']);
for($i=0; $i<$cpt; $i++)
{
if($files['med_file']['name'][$i] !="")
{
$_FILES['med_file']['name']= $files['med_file']['name'][$i];
$_FILES['med_file']['type']= $files['med_file']['type'][$i];
$_FILES['med_file']['tmp_name']= $files['med_file']['tmp_name'][$i];
$attachment_name=$files['med_file']['name'][$i];
$path_info=pathinfo($attachment_name);
$file_extension=@$path_info['extension'];
$path_part_filename=$path_info['filename'];
$rename_file=str_replace(" ","",$path_part_filename).'_'.date('Ymdhis');
if(!empty($rename_file))
{
$_FILES['med_file']['name'] = $rename_file.'.'.$file_extension;
$medical_history_files[]=$rename_file.'.'.$file_extension;
if($this->upload->do_upload('med_file'))
{
$file_upload='true';
}
else if(!$this->upload->do_upload('med_file'))
{
$file_upload="fail";
$error= $this->upload->display_errors();
$this->session->set_flashdata('sucess', $error);
}
}
}
}
}
}
and my view page code is like this.
<form method="post" name="medicalhistory" id="medicalhistory"
enctype="multipart/form-data">
<input id="med_file" type="file" name="med_file[]" multiple>
</form>
Please help me how to solve this problem. Thanks
Upvotes: 1
Views: 1839
Reputation: 12142
You missed a couple of things here.. First of all, your HTML form
should have the attribute action
pointing to your controller method. Second, the $_FILES
array should always contain the following: name, type, tmp_name, error, size
, however, in your loop you are only rebuilding with name, type, tmp_name,
and you are forgetting the others. You are also renaming the file prior its being sent to the upload library. You should do this by setting it in the config
array that is being sent to the library. I would redo you code in the following manner:
Step 1: Make sure the HTML form has the action attribute:
<form action="<?= base_url()?>controller/upload" ..
Step 2: Retrieve the files and unset the original $_FILES so that you can rebuild the array:
$uploaded_files = $_FILES['med_file'];
unset($_FILES);
Step 3: loop through the obtained files and rebuild the $_FILES array into a multidimensional array:
foreach ($uploaded_files as $desc => $arr) {
foreach ($arr as $k => $string) {
$_FILES[$k][$desc] = $string;
}
}
Step 4: Load the Upload library, and set your config options
$this->load->library('upload');
$config['upload_path'] = './medical_history_doc/';
$config['allowed_types'] = 'jpg|jpeg|png|doc|docx|pdf|txt';
Step 5: Loop through the new $_FILES array, rename you file and set the config['filename']
to the new name. Initialize your upload, then run it:
foreach ($_FILES as $k => $file) {
$path_info = pathinfo($file["name"]);
$file_extension = $path_info['extension'];
$path_part_filename = $path_info['filename'];
$config['file_name'] = str_replace(" ", "", $path_part_filename) . '_' . date('Ymdhis') . '.' . $file_extension;
$this->upload->initialize($config);
if (!$this->upload->do_upload($k)) {
$errors = $this->upload->display_errors();
var_dump($errors);
} else {
var_dump("success");
}
}
FINAL RESULT:
View:
<form action="<?= base_url()?>controller/upload" method="post" id="medicalhistory" enctype="multipart/form-data">
<input id="med_file" type="file" name="med_file[]" multiple>
<input type="submit">
</form>
Controller:
public function upload() {
$uploaded_files = $_FILES['med_file'];
unset($_FILES);
foreach ($uploaded_files as $desc => $arr) {
foreach ($arr as $k => $string) {
$_FILES[$k][$desc] = $string;
}
}
$this->load->library('upload');
$config['upload_path'] = './medical_history_doc/';
$config['allowed_types'] = 'jpg|jpeg|png|doc|docx|pdf|txt';
foreach ($_FILES as $k => $file) {
$path_info = pathinfo($file["name"]);
$file_extension = $path_info['extension'];
$path_part_filename = $path_info['filename'];
$config['file_name'] = str_replace(" ", "", $path_part_filename) . '_' . date('Ymdhis') . '.' . $file_extension;
$this->upload->initialize($config);
if (!$this->upload->do_upload($k)) {
$errors = $this->upload->display_errors();
var_dump($errors);
} else {
var_dump("success");
}
}
}
Upvotes: 2