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;
Related
<?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'));
}
}
}
//
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++;
}
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;
}
}
i want to upload multiple images and display related images but iam getting an error you didnt select any file to upload
my view page is this
<div class="form-group col-md-12">
<label class="form-group col-md-3" for="exampleInputEmail1">Image</label>
<div class="form-group col-md-6">
<input type="file" class="" id="image" name="image[]">
</div>
</div>
my controler looks like this...
public function add_images()
{
$data['active_mn']='add_images';
if($_POST)
{
$this->form_validation->set_rules('title','title','required');
$this->form_validation->set_rules('parentname','parent category','required');
$this->form_validation->set_rules('subname','sub category','required');
$this->form_validation->set_rules('code','code','required');
$this->form_validation->set_rules('description','Description','required');
$this->form_validation->set_rules('size','Size','required');
if($this->form_validation->run()==true)
{
if($this->do_upload())
{
$stat=$this->roxmodel->add_images($this->upload->file_name);
if($stat)
{
$data=array();
$colors=array_filter($this->session->userdata('colors'));
/*creating a multidiemensional array to batch insert*/
foreach ($colors as $color)
{
$new_array=array('product_id'=>$stat,'color'=>$color);
array_push($data, $new_array);
}
$product=$this->roxmodel->add_product_color($data);
$this->session->unset_userdata('colors');
$this->session->set_flashdata('message', 'Gallery added Successfully.');
redirect('admin_control/view_images');
}
else
{
$this->session->set_flashdata('message', 'Insertion Failed.');
redirect('admin_control/add_images');
}
//redirect('admin_control/view_images');
}
}
}
$data['parent']=$this->roxmodel->get_parentcategory();
$this->load->view('add_images',$data);
}
my uploaded path looks like this..
function image_upload()
{
$name_array=array();
$count = count($_FILES['image']['size']);
foreach($_FILES as $key => $value)
{
for ($s = 0; $s < $count; $s++)
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['max_width'] = '4000';
$config['max_height'] = '6500';
$config['file_name'] ='img'.time();
$this->load->library('upload', $config);
if(! $this->upload->do_upload("image"))
{
$this->session->set_flashdata('message',$this->upload->display_errors());
return false;
}
else
{
$config['image_library'] = 'gd2';
$config['source_image'] = './images/'.$this->upload->file_name;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 540;
$config['height'] = 660;
$config['new_image'] = './images/thumb/';
$this->load->library('image_lib', $config);
if(! $this->image_lib->resize())
{
$this->session->set_flashdata('message',$this->image_lib->display_errors());
return false;
}
else
{
return true;
}
}
}
}
}
Try like this to upload multiple images .. and please add enctype='multipart/form-data' in your form
$name_array=array();
$count = count($_FILES['image']['size']);
foreach($_FILES as $key => $value)
{
for ($s = 0; $s < $count; $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'] = "uploads/item_images/itemimage/original/";//your imagepath here
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '8000';
$config['max_width'] = '10240';
$config['max_height'] = '7680';
$this->load->library('upload', $config);
if ($this->upload->do_upload("image"))
{
$data = $this->upload->data();
if ($this->image_moo->errors)
{
print $this->upload->display_errors();
}
else
{
$name_array[] = $data['file_name'];
} } }
By default codeIgniter doesn't support multi-file upload. So you can use this library
CodeIgniter Multiple Upload Library
Codeigniter by 3 I am trying to create a controller for multiple uploads.
My vontroller:
public function do_upload($path)
{
$files = $_FILES['file'];
$num_file = count($_FILES['file']['name']);
echo $num_file;
for($i=0; $i<=$num_file; $i++)
{
if(isset($files['name'][$i]))
{
echo $this->session->userdata('dir_corso');
$this->_CI->session->set_userdata(array('filename'=> $files['name'][$i]));
$this->load->library('upload', $this->set_upload_options());
$this->upload->initialize($this->set_upload_options());
chmod($path, 0777);echo '<br/>'.$this->_CI->session->userdata('filename');
$this->upload->do_upload($files['name'][$i]);
$error = array('error' => $this->upload->display_errors());
foreach($error as $errore) echo $errore;
public function set_upload_options(){
$config['upload_path'] = $this->session->userdata('dir_corso');
$config['allowed_types'] = 'gif|jpg|png|GIF|JPG|PNG';
$config['overwrite'] = TRUE;
if(!empty($this->session->userdata('filename'))){
$config['file_name'] = $this->session->userdata('filename');
}
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$config ['encrypt_name'] = TRUE;
return $config; }}}
I'm not getting any error.
Why do not upload files?
Help me.
Function inside another function?? hope that is the issue here,
Try this,
public function do_upload($path) {
$files = $_FILES['file'];
$num_file = count($_FILES['file']['name']);
echo $num_file;
for ($i = 0; $i <= $num_file; $i++) {
if (isset($files['name'][$i])) {
echo $this->session->userdata('dir_corso');
$this->_CI->session->set_userdata(array('filename' => $files['name'][$i]));
$this->load->library('upload', $this->set_upload_options());
$this->upload->initialize($this->set_upload_options());
chmod($path, 0777);
echo '<br/>' . $this->_CI->session->userdata('filename');
$this->upload->do_upload($files['name'][$i]);
$error = array('error' => $this->upload->display_errors());
foreach ($error as $errore)
echo $errore;
}
}
}
public function set_upload_options() {
$config['upload_path'] = $this->session->userdata('dir_corso');
$config['allowed_types'] = 'gif|jpg|png|GIF|JPG|PNG';
$config['overwrite'] = TRUE;
if (!empty($this->session->userdata('filename'))) {
$config['file_name'] = $this->session->userdata('filename');
}
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$config ['encrypt_name'] = TRUE;
return $config;
}