Hello there,
Before trying to check and correct your codes which probably failed and that's why you're writing here, let me post an example which works for me:
for VIEW :
PHP Code:
<div class="dropzone-upload-image">
<form action="<?php echo base_url('admin/add_image_for_project/'.$this->uri->segment(3).''); ?>"
class="dropzone" id="my-awesome-dropzone" >
</form>
</div>
$this->uri->segment(3) is where my ID was in the url. That's why I needed.
And don't forget to add the necessary Js parts on top :
<script src="<?php echo base_url('necessary-parts-admin/js/jquery.js');?>"></script>
<script src="<?php echo base_url('necessary-parts-admin/js/dropzone.js');?>"></script>
<script src="<?php echo base_url('necessary-parts-admin/js/mine.js');?>"></script>
<link rel="stylesheet" href="<?php echo base_url('necessary-parts-admin/js/css/dropzone.css')?>" />
within mine.js I have a feedback to see what has been uploaded:
Code:
Dropzone.autoDiscover = false;
$(function(){
var myDropzone = new Dropzone('#my-awesome-dropzone');
myDropzone.on("complete", function(file){
var img = "<img src='http://localhost/my_project_name/upload/projects/" + file.name + "' />" ;
var tr= "<div class='images'>" + img + "</div>";
$('#uploaded-images').append(tr);
});
});
Controller :
PHP Code:
public function add_image_for_project($id){
$config['upload_path'] = 'upload/projects/'; // I'm not uploading within Codeigniter folders
$config['overwrite'] = False;
$config['allowed_types'] = "jpg|gif|png";
//$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('file')){
//echo $this->upload->data("file_name");
$file_name = $this->upload->data("file_name");
$data = array(
'project_id' => $id,
'project_image_name' => $file_name,
'project_image_link' => base_url(array('upload/projects/'.$this->upload->data("file_name")))
);
$this->load->model('My_Backend_model');
$result = $this->My_Backend_model->lets_add_project_image($data);
if($result){
$this->session->set_flashdata('success', 'Your Image has been uploaded! ');
redirect($_SERVER['HTTP_REFERER']);
}
}
}
MODEL:
PHP Code:
public function lets_add_project_image($data = array()){
$query = $this->db->insert('project_images',$data);
return $query;
}
And don't forget to configure your config/autoload line (most probably 61) : UPLOAD is important
$autoload['libraries'] = array('database','session', 'upload');
PLUS. Let me give you a tip: If you feel stucked about last inserted id bla bla, place "input form" and "image upload" forms on different pages. And redirect the user to the "image upload" page after s/he has posted the form successfully... In short, if post is success redirect to image_upload_page with an url which contains the id
Regards.