for loop does not upload all images in Codeigniter3 - php

I have website where I upload several photos at a time. In some moment I noticed that, it does not upload all required photos. Then I renamed photos from 1.jpg to 14.jpg in order to see, what does my code upload. It uploads only odd numbers (2.jpg, 4.jpg, 6.jpg ... 14.jpg). Where can be the problem?
public function addpic() {
$manymanyimages = '';
$config['upload_path'] = './assets/img/cars/';
$config['allowed_types'] = 'jpg|png|jpeg|JPG|PNG|JPEG';
$config['overwrite'] = FALSE;
$config['remove_spaces'] = TRUE;
$config['max_size'] = '25000';
$this->load->library('upload', $config);
$filesCount = count($_FILES['files']['name']);
for ($i = 0; $i < $filesCount; $i++) {
$_FILES['filee']['name'] = $_FILES['files']['name'][$i];
$_FILES['filee']['type'] = $_FILES['files']['type'][$i];
$_FILES['filee']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
$_FILES['filee']['error'] = $_FILES['files']['error'][$i];
$_FILES['filee']['size'] = $_FILES['files']['size'][$i];
if (!$this->upload->do_upload('filee')) {
// $this->output->set_status_header(500);
$this->output->set_output(strip_tags($this->upload->display_errors()));
} else {
$fileData = $this->upload->data();
$manyimages = $fileData['file_name'];
}
}
$last_id = $this->db->select('id')->order_by('id',"desc")->limit(1)->get('cars')->row()->id + 1;
$data = array(
'img_name' => $manyimages,
'iki' => $manymanyimages,
'post_id' => $last_id
);
return $this->db->insert('imgs', $data);
}

remove for loop and add foreach
$i = 0;
foreach($_FILES['files']['name'] as $row){
$_FILES['filee']['name'] = $_FILES['files']['name'][$i];
$_FILES['filee']['type'] = $_FILES['files']['type'][$i];
$_FILES['filee']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
$_FILES['filee']['error'] = $_FILES['files']['error'][$i];
$_FILES['filee']['size'] = $_FILES['files']['size'][$i];
if (!$this->upload->do_upload('filee')) {
// $this->output->set_status_header(500);
$this->output->set_output(strip_tags($this->upload->display_errors()));
} else {
$fileData = $this->upload->data();
$manyimages = $fileData['file_name'];
}
$i++;
}

Related

unlink gives no such file or directory warning after successfully deleting multiple images in PHP and Codeigniter

<?php
//Controller Code to update gallery images.
$gallery_images = $data['$previous_product_gallery_images'] = $this->ProductModel->get_gallery_images($this->input->post('product_id'));
//Update Gallery Images
$count_gallery_images = count($_FILES['gallery_images']['name']);
if ($count_gallery_images) {
for ($i = 0; $i < $count_gallery_images; $i++) {
foreach ($gallery_images as $gallery_image) {
$file = './assets/images/gallery_images/'.$gallery_image['gallery_image_name'];
chmod('./assets/images/gallery_images', 0777);
unlink($file);
}
$_FILES['userfile']['name'] = $_FILES['gallery_images']['name'][$i];
$_FILES['userfile']['type'] = $_FILES['gallery_images']['type'][$i];
$_FILES['userfile']['tmp_name'] = $_FILES['gallery_images']['tmp_name'][$i];
$_FILES['userfile']['error'] = $_FILES['gallery_images']['error'][$i];
$_FILES['userfile']['size'] = $_FILES['gallery_images']['size'][$i];
$config = array();
$config['upload_path'] = './assets/images/gallery_images';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '100000';
$config['max_width'] = '5000';
$config['max_height'] = '5000';
$this->load->library('upload', $config, 'gallery_image');
$this->gallery_image->initialize($config);
$upload_gallery_image = $this->gallery_image->do_upload('userfile');
if ($upload_gallery_image) {
$gallery_image_name = $_FILES['userfile']['name'];
$this->ProductModel->update_gallery_image($gallery_image_name, $this->input->post('product_id'));
}
}
}
//

Dropzone error in Codeigniter3

I use Dropzone in my CI project. However, I cannot manage it and my files do not upload. It even does not pass image name to variable, as it gives NULL while inserting the database.
Here is my view:
<div id="dropzone" class="dropzone"></div>
<script type="text/javascript">
Dropzone.autoDiscover = false;
var base_url = "<?php echo base_url();?>";
$("div#dropzone").dropzone({
url: base_url + 'cars/addpic',
addRemoveLinks: true,
uploadMultiple: true,
paramName: "files",
acceptedFiles: "image/*",
dictDefaultMessage: "<span class='mif-file-upload mif-3x'></span> <br>Faykkari sechin gorek"
});
</script>
Here is my controller:
public function addpic() {
$config['upload_path'] = './assets/img/cars/';
$config['allowed_types'] = 'jpg|png|jpeg|JPG|PNG|JPEG';
$config['overwrite'] = FALSE;
$config['remove_spaces'] = TRUE;
$config['max_size'] = '25000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('files'))
{
//for multiple
$filesCount = count($_FILES['files']['name']);
for($i = 0; $i < $filesCount; $i++){
$_FILES['filee']['name'] = $_FILES['files']['name'][$i];
$_FILES['filee']['type'] = $_FILES['files']['type'][$i];
$_FILES['filee']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
$_FILES['filee']['error'] = $_FILES['files']['error'][$i];
$_FILES['filee']['size'] = $_FILES['files']['size'][$i];
if(!$this->upload->do_upload('filee')){
$manyimages = '';
} else {
$fileData = $this->upload->data();
$manyimages[] = $fileData['file_name'];
}
}
$manymanyimages= implode(',', $manyimages);
}
and then I pass this data to the main function where all data is uploaded to the database.
public function create($manymanyimages) {
//some other stuff here
$data = array(
'photos' => $manymanyimages
);
$this->cars_model->add_car($data);
redirect('');
}
Try this code; I think your main issue was the nested do_upload() and improper dz handling of errors.
$config['upload_path'] = './assets/img/cars/';
$config['allowed_types'] = 'jpg|png|jpeg|JPG|PNG|JPEG';
$config['overwrite'] = FALSE;
$config['remove_spaces'] = TRUE;
$config['max_size'] = '25000';
$this->load->library('upload', $config);
$manyimages = array();
$filesCount = count($_FILES['files']['name']);
for ($i = 0; $i < $filesCount; $i++) {
$_FILES['filee']['name'] = $_FILES['files']['name'][$i];
$_FILES['filee']['type'] = $_FILES['files']['type'][$i];
$_FILES['filee']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
$_FILES['filee']['error'] = $_FILES['files']['error'][$i];
$_FILES['filee']['size'] = $_FILES['files']['size'][$i];
if (!$this->upload->do_upload('filee')) {
$this->output->set_status_header(500);
$this->output->set_output(strip_tags($this->upload->display_errors()));
} else {
$fileData = $this->upload->data();
$manyimages[] = $fileData['file_name'];
}
}
$manymanyimages = implode(',', $manyimages);
echo $manymanyimages;

Why is multiple uploading not getting inserted into the database

Here I am trying to insert multiple images into the database but they are not inserted. The images are uploading to the folder correctly but don't know why its not entering in to the database.
Here is my image upload function
public function multiple_upload_files($path)
{
$images = array();
if(!empty($_FILES['files']['name'])){
$filesCount = count($_FILES['files']['name']);
for($i = 0; $i < $filesCount; $i++){
$_FILES['file']['name'] = $_FILES['files']['name'][$i];
$_FILES['file']['type'] = $_FILES['files']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['files']['error'][$i];
$_FILES['file']['size'] = $_FILES['files']['size'][$i];
$config['upload_path']= './uploads/'.$path.'/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size']= '2000';
$config['max_width'] = '4000';
$config['max_height'] = '6500';
$config['file_name']='upld-file'.time();
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload("file")) {
$images[] = $this->upload->data();
}
else {
redirect('admin/view-product');
}
}
return $images;
}
Here is my inserting code
$path='products';
if($this->multiple_upload_files($path))
{
$img=implode(',',$images);
$data = array('product_name' => $this->input->post('product_name'),'image'=>$img);
$status = $this->Admin_model->db_insert($table='products',$data);
if($status)
{
$this->session->set_flashdata('message','Product added Successfully');
}
else
{
$this->session->set_flashdata('message','Insertion failed');
}
The problem is the images are uploaded to the uploaded folder but not to the database.
You are returning array of images from method but not receiving it. change you code as below
$path='products';
$images= $this->multiple_upload_files($path);
if($images)
{
$img=implode(',',$images);
$data = array('product_name' => $this->input->post('product_name'),'image'=>$img);
$status = $this->Admin_model->db_insert($table='products',$data);
if($status)
{
$this->session->set_flashdata('message','Product added Successfully');
}
else
{
$this->session->set_flashdata('message','Insertion failed');
}

codigniter: always throw You did not select a file to upload. not able to update

Hello i need help below is my code
It doesn't update any think except the avatar always display error You did not select a file to upload.
don't know how to solve it please help
if($this->input->post()){
$this->form_validation->set_rules('avatar','avatar','callback_multiple_image_upload|trim');
$this->form_validation->set_rules('overview','overview','trim');
$this->form_validation->set_rules('history','history','trim');
$this->form_validation->set_rules('education','education','trim');
$this->form_validation->set_rules('joining','joining','trim');
$this->form_validation->set_rules('hobbies','hobbies','trim');
$this->form_validation->set_rules('experience','experience','trim');
$this->form_validation->set_rules('skills','skills','trim');
/* if($this->input->post('avatar')){
$this->form_validation->set_rules('avatar','avatar','callback_multiple_image_upload|trim');
}else{} */
if($this->form_validation->run())
{
// check if avatar is uploaded
$images = ($this->session->userdata('uploaded_images'))?$this->session->userdata('uploaded_images'):array();
if($this->session->userdata('uploaded_avatar')){
$user['avatar'] = $this->session->userdata('uploaded_avatar');
$this->session->unset_userdata('uploaded_avatar');
}
$data = array(
'avatar' => ($images)?$images[0]['file_name']:'',
'thumb' => ($images)?$images[0]['thumb']:'',
'overview' =>$this->input->post('overview'),
'history' =>$this->input->post('history'),
'education' =>$this->input->post('education'),
'joining' =>$this->input->post('joining'),
'hobbies' =>$this->input->post('hobbies'),
'experience' =>$this->input->post('experience'),
'skills' =>$this->input->post('skills'),
);
$abc = $this->user_model->update($user_id,$data,'user_id');
$this->session->set_flashdata('success','User updated successfully');
redirect('admin/staff/edit/'.$user_id);
}
}
public function multiple_image_upload()
{
$config = array();
$config['upload_path'] = './uploads/user_avatars/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000;
$config['max_width'] = 10240;
$config['max_height'] = 7680;
//$images = array();
if(isset($_FILES['avatar']) && ($count = count($_FILES['avatar']['name'])) > 0)
{
$files = $_FILES;
$images = array();
/* check if folder with year exists*/
$current_year = date('Y');
$path = './uploads/user_avatars/'.$current_year;
if(is_dir($path)){
/* do nothing */
}else{
/* create directory */
#mkdir( $path , 0755, true );
}
$images = array();
for($i = 0; $i < $count; $i++)
{
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '100000';
$config['max_width'] = '102400';
$config['max_height'] = '76800';
$this->load->library('upload', $config);
if ($this->upload->do_upload('avatar'))
{
$data = $this->upload->data();
/* create thumbnail */
$this->load->helper('image_thumb_helper');
generate_image_thumb($data['full_path'],$path,$data['file_name']);
/* add to database */
$images[$i]['file_name'] = $current_year.'/'.$data['file_name'];
$images[$i]['thumb'] = $current_year.'/thumb_'.$data['file_name'];
}else{
$this->form_validation->set_message('multiple_image_upload',$this->upload->display_errors());
return false;
}
}
$this->session->set_userdata('uploaded_images',$images);
return true;
}
else{
return true;
}
}
please provide me solution m not able to remove this error :(
I thing you are using multifile upload. please try bellow one for "for loop"
for($i = 0; $i < $count; $i++)
{
$_FILES['avatar_solo']['name'] = $_FILES['avatar']['name'][$i];
$_FILES['avatar_solo']['type'] = $_FILES['avatar']['type'][$i];
$_FILES['avatar_solo']['tmp_name'] = $_FILES['avatar']['tmp_name'][$i];
$_FILES['avatar_solo']['error'] = $_FILES['avatar']['error'][$i];
$_FILES['avatar_solo']['size'] = $_FILES['avatar']['size'][$i];
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '100000';
$config['max_width'] = '102400';
$config['max_height'] = '76800';
$this->load->library('upload', $config);
if ($this->upload->do_upload('avatar_solo'))
{
$data = $this->upload->data();
/* create thumbnail */
$this->load->helper('image_thumb_helper');
generate_image_thumb($data['full_path'],$path,$data['file_name']);
/* add to database */
$images[$i]['file_name'] = $current_year.'/'.$data['file_name'];
$images[$i]['thumb'] = $current_year.'/thumb_'.$data['file_name'];
}else{
$this->form_validation->set_message('multiple_image_upload',$this->upload->display_errors());
return false;
}
}

Not able to insert path for multiple images in database table using codeigniter

I can't seem to figure out how to make this multiple image path upload working. I have been trying to fix it for 2 days but no luck.
Problem: when form is submitted it uploads selected numbers of images to 'upload' folder but only inserts path for one image in db table.
// Form Validation Goes Here
} else {
// Image upload starts here
$number_of_files = count($_FILES['uploadedimages']['tmp_name']);
$files = $_FILES['uploadedimages'];
for($i=0;$i<$number_of_files;$i++) {
if($_FILES['uploadedimages']['error'][$i] != 0) {
$this->form_validation->set_message('fileupload_check', 'At least 1 image needed.');
return FALSE;
}
}
$this->load->library('upload');
$config['upload_path'] = FCPATH . 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['uploadedimage']['name'] = $files['name'][$i];
$_FILES['uploadedimage']['type'] = $files['type'][$i];
$_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
$_FILES['uploadedimage']['error'] = $files['error'][$i];
$_FILES['uploadedimage']['size'] = $files['size'][$i];
$this->upload->initialize($config);
if ($this->upload->do_upload('uploadedimage')) {
$data['uploadedimage'] = $this->upload->data();
$image_name = $data['uploadedimage']['file_name'];
$data['uploadedimage'] = $image_name;
} else {
$this->form_validation->set_message('upload_error', $this->upload->display_errors());
return FALSE;
}
}
$this->load->model('admin/model_users');
if($query = $this->model_users->insert_property_details($data)) {
redirect('dashboard/property-successfully-posted');
}
Model is:
$insert_images = array(
'property_images' => $data['uploadedimage'],
'property_ref_id' => $id,
);
$this->db->insert('vbc_property_images', $insert_images);
And field name in view file is 'uploadedimage'.
<input type="file" name="uploadedimages[]" accept="image/*" multiple />
$data['uploadedimage'] should be in foreach( $data['uploadedimage'] as $key==>$val) loop for read multiple data.and use 'property_images' => $val for insert data
$data['uploadedimage'] = $image_name;
replace with
$data['uploadedimage'][] = $image_name;
I was able to achieve it by replacing in controller:
if ($this->upload->do_upload('uploadedimage')) {
$data['uploadedimage'] = $this->upload->data();
$image_name = $data['uploadedimage']['file_name'];
$data['uploadedimage'] = $image_name;
}
by
if ($this->upload->do_upload('uploadedimage', $i)) {
$data['uploadedimage'] = $this->upload->data();
$image_name[$i] = $data['uploadedimage']['file_name'];
$data['images'] = implode(',',$image_name);
}
And in model:
'property_images' => $data['uploadedimage'],
by
'property_images' => $data['images'],

Categories