I am using the function below to upload my image files to my server(Localhost). It is fine and image is being uploaded. But I need two image fields and hence both the images should be uploaded once the submit button is clicked. I used the function described here Codeigniter multiple file upload , but it is not working.
I get this error message
A PHP Error was encountered
Severity: Warning
Message: is_uploaded_file() expects parameter 1 to be string, array given
Filename: libraries/Upload.php
Line Number: 161
Well I cannot understand where the error is. The function that I am using to upload single image is
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpeg|png|gif';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
//failed display the errors
}
else
{
//success
}
}
In addition I also like to ask can i change the input field name of my choice. i.e i always need to implement <input type="file" name="userfile"/>
. Is it possible to change the name? I tried changing it and I get the message No file was selected, so that must mean that I cannot change it.
You have to loop through the uploaded files like it is shown in your provided link.
function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpeg|png|gif';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
foreach ($_FILES as $key => $value) {
if (!empty($value['tmp_name'])) {
if ( ! $this->upload->do_upload($key)) {
$error = array('error' => $this->upload->display_errors());
//failed display the errors
} else {
//success
}
}
}
}
And try using HTML like this:
<input type="file" name="file1" id="file_1" />
<input type="file" name="file2" id="file_2" />
<input type="file" name="file3" id="file_3" />
You can change the names of the input fields as you like.
If you are using file multi-select, then do this:
HTML (HTML5 file input with array name allows to select multiple files):
<input type="file" name="userfile[]"/>
or
<input type="file" name="userfile[]"/>
<input type="file" name="userfile[]"/>
PHP in CodeIgniter:
// load upload library (put your own settings there)
$this->load->library('upload', array('allowed_types' => 'svg|gif|jpg|png', 'upload_path' => $GLOBALS['config']['upload_path'], ));
// normalise files array
$input_name = 'userfile'; // change it when needed to match your html
$field_names = array('name', 'type', 'tmp_name', 'error', 'size', );
$keys = array();
foreach($field_names as $field_name){
if (isset($_FILES[$input_name][$field_name])) foreach($_FILES[$input_name][$field_name] as $key => $value){
$_FILES[$input_name.'_'.$key][$field_name] = $value;
$keys[$key] = $key;
}
}
unset($_FILES[$input_name]); // just in case
foreach ($keys as $key){
$new_file = $this->upload->do_upload($input_name.'_'.$key);
// do your stuff with each uploaded file here, delete for example:
$upload_data = $this->upload->data();
unlink($upload_data['file_name']);
}