I am trying ti upload image in the form. All the data in form can be stored into the database accept the images. And the images is not insert into the upload_path also. I wan to make like when user fill in all the form information and upload an image, user click the submit button and all the data including image store into database. What wrong with my code? thank you.
//view.php
<?php echo form_open_multipart('writereview/create');?>
<label for="sitename"><span>Sitename <span class="required">*</span></span><input type="text" class="input-field" name="sitename" value="" /></label>
<input type="file" name="images" size="20" /> <br />
<label><span> </span><input type="submit" value="Submit" /></label>
//controller
public function create() //insert data
{
$data['title'] = 'Write Review'; // Capitalize the first letter
$this->load->helper(array('html', 'url', 'form'));
$this->config_model->writereview();
$this->load->view('templates/header', $data);
$this->load->view('writereview/writereview');
$this->load->view('templates/footer');
}
//model
public function writereview()
{
$this->load->helper('url');
$type = explode('.', $_FILES["pic"]["name"]);
$type = strtolower($type[count($type)-1]);
$url = "./assets/images/".uniqid(rand()).'.'.$type;
if(in_array($type, array("jpg", "jpeg", "gif", "png")))
if(is_uploaded_file($_FILES["pic"]["tmp_name"]))
if(move_uploaded_file($_FILES["pic"]["tmp_name"],$url))
return $url;
$data = array(
'sitename' => $this->input->post('sitename'),
'images'=>$this->input->post('images')
);
return $this->db->insert('review', $data);
}
change your insert data array
$data = array(
'sitename' => $this->input->post('sitename'),
'images'=>$uploaded_file_name
);
also change $_FILES["pic"]["name"] to $_FILES["images"]["name"]
there is no post key named images as the input type is file ...
Here is the complete code for your example...
$image_name ='';
if (isset($_FILES['images']['tmp_name']) && $_FILES['images']['tmp_name'] != '')
{
$config['upload_path'] = 'assets/images/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '20000'; // change it to your max file size
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('images')) {//if upload error
print_r($this->upload->display_errors());//print error
}else{
$data = $this->upload->data();
$image_name = $data['file_name'];
}
}
$data = array(
'sitename' => $this->input->post('sitename'),
'images'=>$image_name
);
return $this->db->insert('review', $data);