Multiple ImageUpload in Code Igniter - php

I have problem in uploading multiple Image in codeigniter
myview
<tr>
<td> Picture</td>
<td>:</td>
<td><input type="file" name="userfile[]" multiple /></input></td>
</tr>
and my model
function multi_upload_files($imagepath=''){
$number_of_files_uploaded = count($_FILES['userfile']['name']);
for ($i = 0; $i < $number_of_files_uploaded; $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'] = $imagepath;
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 900;
$config['max_width'] = 2024;
$config['max_height'] = 1068;
$this->upload->initialize($config);
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$images[]= $uploadData[$i]['file_name'];
return $images;
} }
I have testing it and the problem this code just read my upload Image just 1 image, not equal to number of my file that just upload (for ex: 2 image)

If you look at the following lines, you just overwrote everything with the first item in the array. Thus, only the first item is processed and the rest are overwritten. Plus, you have a return at the end of the loop. The function returns before the loop can finish.
$_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];
...
return $images;

You miss two things.
First,variable $imagepath is not defined.
Second, You miss to load upload libaray.
So,Solution is here try it.Hope it will works...
function multi_upload_files($imagepath=''){
$data = array();
$number_of_files_uploaded = count($_FILES['userfile']['name']);
for ($i = 0; $i < $number_of_files_uploaded; $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];
$imagepath = 'Define upload path here';
$config['upload_path'] = $imagepath;
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 900;
$config['max_width'] = 2024;
$config['max_height'] = 1068;
//Load library first then use
$this->load->library('upload');
$this->upload->initialize($config);
if($this->upload->do_upload('userfile'))
{
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$images[]= $uploadData[$i]['file_name'];
}
return $images;
}

Related

setting width and height of image in codeigniter php not working

i have a codeigniter website where user can upload multiple images, i want all the images to be of the same size, i have done the following code in controller:
if (isset($_POST['addblog'])) {
$this->load->library('upload');
$image = array();
$ImageCount = count($_FILES['image_name']['name']);
for ($i = 0; $i < $ImageCount; $i++) {
$_FILES['file']['name'] = $_FILES['image_name']['name'][$i];
$_FILES['file']['type'] = $_FILES['image_name']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['image_name']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['image_name']['error'][$i];
$_FILES['file']['size'] = $_FILES['image_name']['size'][$i];
$uploadPath = './uploads/blog/';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['width'] = 200;
$config['height'] = 250;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('file')) {
$imageData = $this->upload->data();
$uploadImgData[] = $imageData['file_name'];
}
}
$blogimage = $uploadImgData;
}
as u can see i have set the height and width in config, the images are still uploaded in ther original size, can anyone please tell me what is wrong in here, thanks in advance
You has just made a little mistake in your config :
$config['max_width'] = 200;
$config['max_height'] = 250;
Sadly "min_width" and "min_height" seems doesn't exist yet

Upload multiple image doesn't store to databased and path folder

I'm making upload multiple image and stored to one field. But I have a proble with my code, images can't store to database and to path folder. May be you can help me please
This is my controller
public function post() {
if(empty($_FILES['file']['name'])) {
$data = array( 'id_merk' => $this->input->post('id_merk'),
'createdAt' => $this->input->post('createdAt')
);
// var_dump($data);
$this->m_barang->post( $data );
$this->session->set_flashdata('success', 'success');
redirect('admin/merk');
} else {
$count = count($_FILES['file']['size']);
foreach($_FILES as $value){
for($s=0; $s<=$count-1; $s++) {
$_FILES['file']['name'] = $value['name'][$s];
$_FILES['file']['type'] = $value['type'][$s];
$_FILES['file']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['file']['error'] = $value['error'][$s];
$_FILES['file']['size'] = $value['size'][$s];
// $config['file_name'] = 'pict_'.date('Y_m_d_H_i_s').'.jpg';
$config['upload_path'] = './upload/be/barang';
$config['allowed_types'] = 'gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG';
$config['max_size'] = '8000';
$config['max_width'] = '1366';
$config['max_height'] = '1024';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
$name_array[] = $data['file_name'];
}
}
$names = implode(', ', $name_array);
$data = array( 'id_merk' => $this->input->post('id_merk'),
'photo_barang' => $names
);
$this->m_barang->post( $data );
$this->session->set_flashdata('success', 'gambar ada');
redirect('admin/merk');
}
}
and this is my view
<input class="form-control" name="file[]" id="files" type="file" multiple="multiple">
Please, can you help me how to fix my problem with my code?
First, Use only 1 loop to upload the multiple images.
Second, give name attribute of <input type="file"> to $this->upload->do_upload()
Third, if uploading path inside the application folder then use APPPATH.'upload/be/barang' or if outside the application folder use FCPATH.'upload/be/barang'
$count = count($_FILES['file']['size']);
for($s=0; $s<=$count-1; $s++) {
$_FILES['file']['name'] = $_FILES['file']['name'][$s];
$_FILES['file']['type'] = $_FILES['file']['type'][$s];
$_FILES['file']['tmp_name'] = $_FILES['file']['tmp_name'][$s];
$_FILES['file']['error'] = $_FILES['file']['error'][$s];
$_FILES['file']['size'] = $_FILES['file']['size'][$s];
$config['upload_path'] = './upload/be/barang';
$config['allowed_types'] = 'gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG';
$config['max_size'] = '8000';
$config['max_width'] = '1366';
$config['max_height'] = '1024';
$this->load->library('upload', $config);
if(!$this->upload->do_upload('file')){
//image uploading error
}else{
$data = $this->upload->data();
$name_array[] = $data['file_name'];
}
}

my issue is that i insert multiple image in one row with comma separated in codeginter

I am so tried to insert to multiple images its work perfectly but what i want to multiple images insert in to database with one row means comma separated like that for example id =4 and img = img1,img2,img3,img4 i want insert in one row in codeginter but don't know how to use this function $data= implode(",",$userfile); THANKS advances brother
Here is my controller function
function blog_img_new()
{
$imgtest = $this->blog->image_get_test();
$this->template->load_sub('imgtest', $imgtest);
$this->template->load('admin/test-imag');
}
function blog_img()
{
$number_of_file = sizeof($_FILES['userfile']['tmp_name']);
$file = $_FILES['userfile'];
// Faking upload calls to $_FILE
for ($i = 0; $i < $number_of_file; $i++) :
$_FILES['userfile']['name'] = $file ['name'][$i];
$_FILES['userfile']['type'] = $file ['type'][$i];
$_FILES['userfile']['tmp_name'] = $file ['tmp_name'][$i];
$_FILES['userfile']['error'] = $file ['error'][$i];
$_FILES['userfile']['size'] = $file ['size'][$i];
$config['upload_path'] = './photo/uploads'; //The path where the image will be save
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload('userfile');
$data = $this->upload->data();
$file_name[] = $this->upload->data();
$data = array(
'userfile' => $this->upload->data('file_name'),
);
$data= implode(",",$userfile);
$this->blog->blog_img($data);
//redirect('/admin/blog/img/insert');
endfor;
}
this is my model function
function blog_img($data)
{
$userfile = addslashes($data['userfile']);
return $this->db->query("INSERT INTO filename_img (userfile) VALUES ('$userfile')");
}
view page
<input type="file" name="userfile[]" id="userfile" multiple >
You have to move the insert function out of the loop. In the loop you have to assign each filename to an array you can implode.
function blog_img()
{
$number_of_file = sizeof($_FILES['userfile']['tmp_name']);
$file = $_FILES['userfile'];
$files = array();
// Faking upload calls to $_FILE
for ($i = 0; $i < $number_of_file; $i++) :
$_FILES['userfile']['name'] = $file ['name'][$i];
$_FILES['userfile']['type'] = $file ['type'][$i];
$_FILES['userfile']['tmp_name'] = $file ['tmp_name'][$i];
$_FILES['userfile']['error'] = $file ['error'][$i];
$_FILES['userfile']['size'] = $file ['size'][$i];
$config['upload_path'] = './photo/uploads'; //The path where the image will be save
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload('userfile');
//$data = $this->upload->data();
$files[] = $this->upload->data('file_name');
//$data= implode(",",$userfile);
//$this->blog->blog_img($data);
//redirect('/admin/blog/img/insert');
endfor;
$data= implode(",",$files);
$this->blog->blog_img($data);
}
Model using QB:
function blog_img($files)
{
$this->db->set('userfile', $files);
return $this->db->insert('filename_img');
}

how to upload multi file type with one form in codeigniter?

I have one multi-part form. I want to upload 5 images and one video with this form. I do not want using AJAX upload.
$this->form_validation->set_rules('file' , 'lang:pic' , 'callback_multiple_upload');
$this->form_validation->set_rules('video' , 'lang:video' , 'callback_video_upload');
For multiple images
$config['upload_path'] = PATH; //add path according to your requirements
$config['allowed_types'] = 'jpg|jpeg|png';
$config['overwrite'] = false; //OR true
$config['max_size'] = '100000'; //You can change it
$this->load->library('upload');
$files = $_FILES;
$number_of_files = count($_FILES['pic']['name']); //"pic" is name of FILE input
//images name will be details0, details1, details2 and soo on.
$errors = 0;
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['pic']['name'] = "details" . $i . ".jpg"; //If you want to change the name of images change "details" with your require name
$_FILES['pic']['type'] = $files['pic']['type'][$i];
$_FILES['pic']['tmp_name'] = $files['pic']['tmp_name'][$i];
$_FILES['pic']['error'] = $files['pic']['error'][$i];
$_FILES['pic']['size'] = $files['pic']['size'][$i];
$this->upload->initialize($config);
if (!$this->upload->do_upload("pic"))
{
$errors++;
}
}
if ($errors > 0)
{
echo $errors . "File(s) could not be uploaded";
}
You can initialize the upload class for each file and set the required file type i.e.
$config['upload_path']= './uploads/files/';
$config['allowed_types'] = 'docx|doc|pdf|txt|odt';
$config['max_size'] = 10000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->upload->initialize($config);
$this->upload->do_upload();
//check errors in first upload.
then for video
$config['upload_path']= './uploads/videos/';
$config['allowed_types'] = 'mp4|flv';
$config['max_size'] = 10000;
$this->upload->initialize($config);
$this->upload->do_upload();
For multiple file uploading
$filesCount = count($_FILES['userFiles']['name']);
for($i = 0; $i < $filesCount; $i++){
$_FILES['userFile']['name'] = $_FILES['userFiles']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['userFiles']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['userFiles']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['userFiles']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['userFiles']['size'][$i];
$upload_path ="/test/assets/upload";
$output_dir = $_SERVER['DOCUMENT_ROOT']. $upload_path;
$config['upload_path'] = $output_dir;
// $config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'jpg|png|mp4; //add other_extensions';
$config['file_name'] = 'PIC_'.$user_id.'_'.$i;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('userFile')){
$fileData = $this->upload->data();
$img_insert_array = array('user_id'=>$user_id,'profile_pic'=> $fileData['file_name']);
$res = $this->Test_model->update_img($img_insert_array );
//here insert in db
echo "uploaded successful";
}
else {
print_r($this->upload->display_errors());
}
}

Codeigniter - How to upload original image with thumbnail image?

I have this code to file upload in codeigniter:
if(!empty($_FILES['userfile'])){
$name_array = array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key => $value)
for ($s=0; $s<=$count-1; $s++){
$_FILES['userfile']['name'] = $value['name'][$s];
$_FILES['userfile']['type'] = $value['type'][$s];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['userfile']['error'] = $value['error'][$s];
$_FILES['userfile']['size'] = $value['size'][$s];
$config['upload_path'] = './public/images/campaign-images/';
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
$config['max_size'] = '10000';
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
$CI->load->library('upload', $config);
$CI->upload->do_upload();
$data = $CI->upload->data();
$name_array[] = $data['file_name'];
}
return $name_array;
}
This code is working perfect with single original image upload but how to upload image as thumbnail image(350 x 250) with resize in different folder.
Any idea how to do with codeigniter library?
Thanks
First store the original image and afterwards thumbnail image will upload
You just need to include gd2 library to generate the thumbnail image
if(!empty($_FILES['userfile'])){
$name_array = array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key => $value)
for ($s=0; $s<=$count-1; $s++)
{
//Original Image Upload - Start
$_FILES['userfile']['name'] = $value['name'][$s];
$_FILES['userfile']['type'] = $value['type'][$s];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['userfile']['error'] = $value['error'][$s];
$_FILES['userfile']['size'] = $value['size'][$s];
$config['upload_path'] = './public/images/campaign-images/';
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
$config['max_size'] = '10000';
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
$CI->load->library('upload', $config);
$CI->upload->do_upload();
$data = $CI->upload->data();
//Original Image Upload - End
//Thumbnail Image Upload - Start
$config['image_library'] = 'gd2';
$config['source_image'] = './public/images/campaign-images/'. $value['name'][$s];
$config['new_image'] = './public/images/campaign-images/thumbs/'.$value['name'][$s];
$config['width'] = 350;
$config['height'] = 250;
//load resize library
$this->load->library('image_lib', $config);
$this->image_lib->resize();
//Thumbnail Image Upload - End
$name_array[] = $data['file_name'];
}
return $name_array;
}
if(!empty($_FILES['userfile'])){
$name_array = array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key => $value)
for ($s=0; $s<=$count-1; $s++)
{
//Original Image Upload - Start
$_FILES['userfile']['name'] = $value['name'][$s];
$_FILES['userfile']['type'] = $value['type'][$s];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['userfile']['error'] = $value['error'][$s];
$_FILES['userfile']['size'] = $value['size'][$s];
$config['upload_path'] = 'PATH_TO_UPLOAD_IMAGE';
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
$config['max_size'] = '10000';
$CI->load->library('upload', $config);
$CI->upload->do_upload();
$data = $CI->upload->data();
//Original Size of Image Upload - End
//Thumbnail Size of Image Upload - Start
$config['image_library'] = 'gd2';
$config['source_image'] = 'PATH_TO_IMAGE_UPLOAD'. $value['name'][$s];
$config['new_image'] = 'PATH_TO_UPLOAD_THUMB_IMAGE'.$value['name'][$s];
$config['width'] = 350;
$config['height'] = 250;
//CI load resize library
$this->load->library('image_lib', $config);
$this->image_lib->resize();
//Thumbnail SIZED Image Upload - End
$name_array[] = $data['file_name'];
}
return $name_array;
}

Categories