SEVERAL IMAGES AT THE SAME TIME WITH CODEIGNITER FILE UPLOAD

Sep

1

September 1 , 2014 | Posted by Igniweb | No Comments

SEVERAL IMAGES AT THE SAME TIME WITH CODEIGNITER FILE UPLOAD

Sometimes it is necessary to double-Upload in our forms, I have encountered the awkward problem that the library File Uploading CodeIgniter only allowed to upload an image form, or in the worst case, both images are with the same name .

A typical piece of code called a do_upload ():

function do_upload($upload_path = '', $allowed_types = '', $filename = '', $redirect_path = ',$file='userfile')
{
$config['upload_path']     = $upload_path;
$config['allowed_types']    = $allowed_types;
$config['max_size']     = '10240';
$config['max_width']     = '6000';
$config['max_height']     = '6000';
$config['file_name']     = $filename;
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error = array(
'error' => $this->upload->display_errors()
);
$this->prepare_flashmessage($error['error'], 1);
redirect($redirect_path, 'refresh');
}
}

A call to the above function:

Although the function is to send a different name for each file, both images were once created with the same name, only with the difference that the automatic renowned places a one (1) at the end of the file name:


This is because once initialized the library, it bears some settings, among them the Input file name (it is almost always ‘userfile’).

To resolve this error must be added the line that initializes the library again, this way is ‘reset’ the same

$this->upload->initialize($config);

Resulting in a method:

function do_upload($upload_path = '', $allowed_types = '', $filename = '', $redirect_path = ',$file='userfile')
{
$config['upload_path']     = $upload_path;
$config['allowed_types']    = $allowed_types;
$config['max_size']     = '10240';
$config['max_width']     = '6000';
$config['max_height']     = '6000';
$config['file_name']     = $filename;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($file)) {
$error = array(
'error' => $this->upload->display_errors()
);
$this->prepare_flashmessage($error['error'], 1);
redirect($redirect_path, 'refresh');
}
}

Add Comment

Your email address will not be published. Required fields are marked *