Uploading Any Files using codeigniter.
Today I am going to tell you that How we can upload multiple files at once.
Step:1
In Today's world website is having lots of image files mostly in eCommerce sites.and we want to upload many images at once. Because of this we can save our time as well as speed.
Create a function upload_files in controller.all type of file you can upload either it is image file,pdf file,doc file or vedio file ,ios supported format file.
copy this code in your controller function.
if(!empty($_FILES['image']['name']['0']))
{
$files = $_FILES;
$config['upload_path'] = './post_file/';
$path=$config['upload_path'];
$config['allowed_types'] = 'gif|jpg|png|jpeg|mp4|3GP|OGG|pdf|csv|txt|doc|xlsx|docx|xls';
$config['max_size'] = '15360';
//$config['encrypt_name'] = TRUE;
//$config['max_width'] = '1920';
//$config['max_height'] = '1280';
$this->load->library('upload', $config);
$count = count($_FILES['image']['name']);
for($i=0; $i<$count; $i++)
{
if (!empty($_FILES['image']['name']))
{
$name = $files["image"]["name"][$i];
$ext = end((explode(".", $name)));
$_FILES['image']['name']= $files['image']['name'][$i];
$_FILES['image']['type']= $files['image']['type'][$i];
$_FILES['image']['tmp_name']= $files['image']['tmp_name'][$i];
$_FILES['image']['error']= $files['image']['error'][$i];
$_FILES['image']['size']= $files['image']['size'][$i];
$rand = rand(1,100).time();
$new_name = $rand.'_'.preg_replace('/[^A-Za-z0-9\_\-.]/', '_',$_FILES["image"]['name']);
$config['file_name'] = $new_name;
$this->upload->initialize($config);
if (!$this->upload->do_upload('image'))
{
$errors = $this->upload->display_errors();
}
else
{
$image_data=array(
'post_id' => $post_id,
'file' => $new_name//$_FILES['image']['name']
);
$this->Admin_model->insert_file($image_data);
}
}
}
}
Step:2
In model write this code for the insert particular file in database.
public function insert_file($image_data)
{
$this->db->insert('tbl_post_file',$image_data);
return ($this->db->affected_rows() != 1) ? False : True;
}
one more thing also create folder post_file in application level of your project then you can see after this code files are uploading or not.if not then also check permission of folder change it to 777.
Hope this code will work .All the best.
If you are facing any problem with this code then contact me on my email and you can also do comment. Please feel free to ask any query for any topic on codeigniter.