codeigniter upload images from different inputs - codeigniter

my code is working fine in the single upload, but I want to make a loop for my code that I won't repeatedly create a function over and over because of using two different inputs.
public function uploadImage_1()
{
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2608';
$config['max_width'] = '2608';
$config['max_height'] = '2608';
$this->load->library('upload', $config);
if ( !$this->upload->do_upload('userfile1')){
$error = array('error' => $this->upload->display_errors());
}else{
$fileName = $this->upload->data();
$post_image = $fileName['file_name'];
return $post_image;
}
}
public function uploadImage_2()
{}
` View
<input type="file" name="userfile1" size="20" required/>
<input type="file" name="userfile2" size="20" required/>`

You can pass the input field name as parameter into your function.
like this:
public function uploadImage($input_field_name)
{
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2608';
$config['max_width'] = '2608';
$config['max_height'] = '2608';
$this->load->library('upload', $config);
if ( !$this->upload->do_upload($input_field_name)){
$error = array('error' => $this->upload->display_errors());
}else{
$fileName = $this->upload->data();
$post_image = $fileName['file_name'];
return $post_image;
}
}
Now use the above function like this:
// These variables stores the name of your files
$fileName_1 = $this->uploadImage('userfile1');
$fileName_2 = $this->uploadImage('userfile2');

Related

codeigniter upload files from multiple input

i want to upload files with multiple input. I have tried the code below, but the files not uploaded, and i cant get the upload directory.
HTML
<input type="file" name="userfile[]" size="20" />
Controller
public function detailTugasInput($idKelas,$id){
$this->load->model('tugass');
$records = $_FILES['userfile'];
//var_dump($records); die();
$config['upload_path'] = './file/';
$config['allowed_types'] = 'pdf|doc|txt|jpg|png';
$config['max_size'] = 0;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
foreach ($_FILES as $key => $value) {
$upload_data = $this->upload->data();
$file_ary = array(
'nama_kembali' => $value['name'],
'file_kembali' => $value['tmp_name']
);
}
}
And i also tried to get the uploaded data with this code below, but why always appear error?
$checked_arr = $_POST['userfile'];
Try Below Codes
HTML :
<input type="file" class="form-control" name="userfile[]" multiple />
Controller :
public function detailTugasInput($idKelas, $id){
$this->load->model('tugass');
if(!empty($_FILES['userfile']['name'])) {
$filesCount = count($_FILES['userfile']['name']);
for($i = 0; $i < $filesCount; $i++) {
$_FILES['userFile']['name'] = $_FILES['userfile']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['userfile']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['userfile']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['userfile']['size'][$i];
$config['upload_path'] = './file/';
$config['allowed_types'] = 'pdf|doc|txt|jpg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload('userFile');
if($this->upload->do_upload('userFile')){
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$uploadData[$i]['created'] = date("Y-m-d H:i:s");
$uploadData[$i]['modified'] = date("Y-m-d H:i:s");
}
}
}
}
For more Support, Check This

image not storing in folder using codeigniter

My code is given below i unable to store image in sepecfic folder in codeigniter. Image is storing database table but not store in the specific path.
Controller:
if (isset($_FILES['header_image']) && $_FILES['header_image']['name'] != "") {
$fileName="header_image_$id";
$ext= end(explode('.',$_FILES['header_image']['name']));
$file_name=$fileName.".".$ext;
$this->_upload('uploads/header_images/', "gif|jpg|png|jpeg", "header_image",$file_name);
$this->generic_model->updateRecord("blood_tips",array("header_image"=>'uploads/header_images/'.$fileName.".$ext"), array("id"=>$id));
}
private function _upload($uploadPath,$allowedTypes,$name,$file_name)
{
$config['file_name'] = $file_name;
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = $allowedTypes;
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload($name))
{
return $this->upload->display_errors();
}
else
{
$data = array('upload_data' => $this->upload->data());
}
}
Your file upload parameter is different than proposed documentation .
$config['upload_path'] = './uploads/'; Notice "./" before path.
te correct way to upload file in CI, you need to make like this:
$fileName="header_image_$id";
$ext= end(explode('.',$_FILES['header_image']['name']));
$config['upload_path'] = './uploads/header_images/';
$config['file_name'] = $fileName.".".$ext;//The extension provided in the file name must also be an allowed file type.
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 0;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}

While Uploading Image don't want to save actual image name with space

In codeigniter while I uploading the image and store image name in database If actual image name is like "abc data.jpg" and I want to store image name like abc_data.jpg and also with this name image should move in uploads folder.
This is my image Upload Code:-
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 5000;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('cover_image'))
{
$data =$this->upload->data();
$data_ary = array(
'project_id' => (int)$iProjectId,
'image_url' => $data['file_name'],
'is_covred_photo' => 'YES'
);
$this->db_project->insert( $this->sTable6 , $data_ary);
$aResp = array( 'project_images_id' => $this->db->insert_id());
}
Add $config['file_name']
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 5000;
$config['file_name'] = 'My_new_file_name'; # Add here
$this->load->library('upload', $config);
remove this line as well
$this->upload->initialize($config);
Try this:
$file_name = $_FILES['file_view_name']['name'];
$file_name_pieces = split('_', $file_name);
$new_file_name = '';
$count = 1;
foreach($file_name_pieces as $piece)
{
if ($count !== 1)
{
$piece = ucfirst($piece);
}
$new_file_name .= $piece;
$count++;
}
$config['file_name'] = $new_file_name;
Refer this

My file type not saving on my database

I have completed file upload but if the file name is "213134.jpg" but my code store database only name "213134" without file type. Can you please help me. I have used CI for this task
function postMessage(){
$config['upload_path'] = './uploads/inbox/';
$config['allowed_types'] = 'jpg';
$config['max_size'] = '2048';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$random=rand(00000, 99999);
$id=$this->session->userdata('id');
$pic=$id*$random;
$config['file_name'] =$pic;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$blog_msg ='Sorry, Picture uploaded.';
}
$u_id=$this->session->userdata('id');
$u_name=$this->session->userdata('u_fname');
$to=$this->uri->segment(3);
$date= $date=date('d-M-Y');
$message=$_POST["message"];
$data=array(
'm_from'=>$u_id,
'from_name'=>$u_name,
'm_date'=>$date,
'm_body'=>$message,
'm_to'=>$to,
'm_attach'=>$pic
);
}
$config['allowed_types'] = 'jpg|jpeg';//Change this
Method 01
$data=array(
'm_from'=>$u_id,
'from_name'=>$u_name,
'm_date'=>$date,
'm_body'=>$message,
'm_to'=>$to,
'm_attach'=>$this->input->post('pic'), //change this(in your HTML attribute name='' )
);
Method 02
or else you can try
$ext = pathinfo($pic, PATHINFO_EXTENSION); //Get the Extension
$random=rand(00000, 99999);
$id=$this->session->userdata('id');
$pic=$id*$random.$ext;//change this
$config['file_name'] =$pic;

How to upload multiple images and crete thumb in codeigniter and also store their name in database with other fileds

I want to upload 3 images and create thumb of them and want to store their name in databse .
Any idea what to do.NOte I have read many forum and questions but none has helped me in doing both upload and store in database.Here is what i have done for 1 image,it would help me if i can do it same way for multiple images with little modification
View
<p>
<label>Image 1</label>
<input type="file" name="userfile" value=""/>
// I want same kind of anothe two uploads say image2,image3
</p>
Controller
function insert()
{
$data = array('title'=>'Add New Image',
'link_add'=>site_url('manage/img_gallaryslider/add'),
'edit_link'=>site_url('manage/img_gallaryslider/edit'), 'action'=>site_url('manage/img_gallaryslider/insert'),
'link_back'=>site_url('manage/img_gallaryslider'),
'tbl'=>'imgslider');
$this->_set_fields();
$this->_set_rules();
if ($this->form_validation->run() == TRUE)
{
$config['upload_path'] = './images/imagegallaryslider';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
if($_FILES['userfile']['name']=='')
{
$data['upload_error']='<strong>Error: Please, select image to upload</strong>';
}
else
{
$data['upload_error']='<strong>Error:Invalid file format or size</strong>';
}
$this->load->view('manage/includes/header', $data);
$this->load->view('manage/img_gallaryslideredit', $data);
$this->load->view('manage/includes/footer');
}
else
{
$data = array('upload_data' => $this->upload->data());
$filename= $this->upload->data();
$file_name=$this->upload->file_name;
$this->create_thumb($file_name);
// save data
$this->ip_date = $this->admin_model->get_date_ip();
$value_array = array('Name' => $this->input->post('name'),
'Image'=>$this->upload->file_name,
'CreatedBy' => $this->session->userdata('adminid'),
'CreatedDate'=>$this->ip_date->cur_date,
'CreatedIp'=>$this->ip_date->ip);
$id = $this->admin_model->save('imggallaryslider',$value_array);
$this->session->set_flashdata('notification',$this->lang->line('gen_succ_added'));
redirect(site_url('manage/img_gallaryslider/index'));
die();
}
}
else
{
$this->load->view('manage/includes/header', $data);
$this->load->view('manage/img_gallaryslideredit', $data);
$this->load->view('manage/includes/footer');
}
}
function create_thumb($file_name)
{
$config['image_library'] = 'gd2';
$config['source_image'] = './images/imagegallaryslider/'.$file_name;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 70;
$config['height'] = 50;
$config['new_image'] = './images/imagegallaryslider/thumb/'.$file_name;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
}
model
function save($table,$value)
{
$this->db->insert($table, $value);
return $this->db->insert_id();
}
</code>
The method do_upload() declared as public function do_upload($field = 'userfile'),
so you can use it in your code like:
private function uploads() {
$config['upload_path'] = './images/imagegallaryslider';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
//<--HERE the several images handler
foreach (array('userfile','img1','img2' as $field) {
if ( ! $this->upload->do_upload($field))
{
$error = array('error' => $this->upload->display_errors());
if($_FILES[$field]['name']=='')
{
$data['upload_error']='<strong>Error: Please, select image to upload</strong>';
}
else
{
$data['upload_error']='<strong>Error:Invalid file format or size</strong>';
}
return $data;
}
$data = array('upload_data' => $this->upload->data());
$filename= $this->upload->data();
$file_name=$this->upload->file_name;
$this->create_thumb($file_name);
// save data
$this->ip_date = $this->admin_model->get_date_ip();
$value_array = array('Name' => $this->input->post('name'),
'Image'=>$this->upload->file_name,
'CreatedBy' => $this->session->userdata('adminid'),
'CreatedDate'=>$this->ip_date->cur_date,
'CreatedIp'=>$this->ip_date->ip);
$id = $this->admin_model->save('imggallaryslider',$value_array);
}
$this->session->set_flashdata('notification',$this->lang->line('gen_succ_added'));
redirect(site_url('manage/img_gallaryslider/index'));
die();
}
in view it should look like:
<p>
<label>Image 1</label>
<input type="file" name="userfile" value=""/>
<input type="file" name="img1" value=""/>
<input type="file" name="img2" value=""/>
</p>

Resources