Reputation: 8705
I have a bit of the problem with file upload. I am trying to upload two files, but for some reason first file is being uploaded twice and there is no trace of the second file. What am I doing wrong?
Function for file upload (model):
public function file_upload($folder, $allowed_type, $max_size = 0, $max_width = 0, $max_height = 0)
{
$folder = $this->path . $folder;
$files = array();
$count = 0;
foreach ($_FILES as $key => $value) :
$file_name = is_array($value['name']) ? $value['name'][$count] : $value['name'];
$file_name = $this->global_functions->char_replace($file_name, '_');
$count++;
$config = array(
'allowed_types' => $allowed_type,
'upload_path' => $folder,
'file_name' => $file_name,
'max_size' => $max_size,
'max_width' => $max_width,
'max_height' => $max_height,
'remove_spaces' => TRUE
);
$this->load->library('image_lib');
$this->image_lib->clear();
$this->load->library('upload', $config);
if (!$this->upload->do_upload($key)) :
$error = array('error' => $this->upload->display_errors());
return FALSE;
else :
$file = $this->upload->data();
$files[] = $file['file_name'];
endif;
endforeach;
if(empty($files)):
return FALSE;
else:
return implode(',', $files);
endif;
}
Function form file upload (controller):
public function dokument_insert()
{
$this->admin_login_check();
$dokument =explode(',', $this->doc->file_upload('/doc', 'pdf|PDF|doc|docx') );
var_dump($dokument);
$data = array(
'naziv' => $this->input->post('naziv_srb'),
'opis' => $this->input->post('opis_srb'),
'path_pdf' => $dokument[0],
'path_doc' => $dokument[1],
'kategorija_id' => $this->input->post('kategorija'),
'jezik_id' => $this->input->post('jezik')
);
$this->doc->save($data);
}
Part of the form for files (view):
<label for="dokument_pdf">Dokument PDF</label>
<input type="file" name="pdf" id="dokument_pdf">
<label for="dokument_doc">Dokument DOC</label>
<input type="file" name="doc" id="dokument_doc">
Upvotes: 1
Views: 980
Reputation: 5230
You must to initialize the upload library with each upload.
$this->load->library('upload');
$this->upload->initialize($config);
User guide: https://www.codeigniter.com/user_guide/libraries/file_uploading.html
Upvotes: 2
Reputation: 410
Looks like because you have 2 doc fields it will ANYWAY loop 2 times, whatever...
If you want only one you should send only one via the form OR don't use a ForEach and process each one manually each other with their fixed names.
Upvotes: 0