I want to insert image(path only) to database.I have written this code but here every time the default.png(path only) is inserted.I want to insert other images also.
$maxId = $this->user_master_m->get_max_product_id();
$new_file = "defualt.png";
if ($_FILES["image"]['name'] != "") {
$file_name = $_FILES["image"]['name'];
$file_name_rename = 'P-' . $maxId->id;
$explode = explode('.', $file_name);
if (count($explode) >= 2) {
$new_file = $file_name_rename . '.' . $explode[1];
$config['upload_path'] = "./uploads/images";
$config['allowed_types'] = "gif|jpg|png";
$config['file_name'] = $new_file;
$config['max_size'] = '1024';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$array['product_image'] = $new_file;
$this->load->library('upload', $config);
if (!$this->upload->do_upload("image")) {
$this->data["image"] = $this->upload->display_errors();
$this->data["subview"] = "admin_view/product_form_view";
$this->load->view('_layout_main', $this->data);
} else {
$data = array("upload_data" => $this->upload->data());
$this->user_master_m->add_product_m($array);
}
} else {
$this->data["image"] = "Invalid file";
$this->data["subview"] = "admin_view/product_form_view";
$this->load->view('_layout_main', $this->data);
}
} else {
$array["product_image"] = $new_file;
$this->user_master_m->add_product_m($array);
}
Try to print $_FILES by
print_r($_FILES);
to check whether you are getting the image properly or not.
Related
i trying to upload file but its still cant insert to database i dont know why, i've try many code but none of it working, much appreciate if you help me
here's my code :
$upload_image_dp1 = $_FILES['upload_dp1']['name'];
$upload_image_dp2 = $_FILES['upload_dp2']['name'];
$upload_image_dp3 = $_FILES['upload_dp3']['name'];
$upload_file_invoice = $_FILES['upload_invoice']['name'];
if (!empty($upload_image_dp1) && !empty($upload_image_dp2) && !empty($upload_image_dp3) && !empty($upload_file_invoice)) {
$config['upload_path'] = './assets/img/uploads/dp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '10240';
$this->load->library('upload', $config);
// $config2['upload_path'] = './assets/img/uploads/invoice/';
// $config2['allowed_types'] = 'pdf';
// $config2['max_size'] = '10240';
// $this->load->library('upload', $config2);
$upload_dp1 = $this->upload->do_upload('upload_dp1');
$file1 = $this->upload->data('file_name');
$upload_dp2 = $this->upload->do_upload('upload_dp2');
$file2 = $this->upload->data('file_name');
$upload_dp3 = $this->upload->do_upload('upload_dp3');
$file3 = $this->upload->data('file_name');
$upload_invoice = $this->upload->do_upload('upload_invoice');
$file4 = $this->upload->data('file_name');
$this->db->set('upload_dp1', $file1);
$this->db->set('upload_dp2', $file2);
$this->db->set('upload_dp3', $file3);
$this->db->set('upload_invoice', $file4);
$this->db->where('id_user', $this->user['id_user']);
$this->db->where('status', "Not Complete");
$this->db->where('id_type', $id_type);
$this->db->update('passanger');
public function upload()
{
$upload_image_dp1 = $_FILES['upload_dp1']['name'];
$upload_image_dp2 = $_FILES['upload_dp2']['name'];
$upload_image_dp3 = $_FILES['upload_dp3']['name'];
$upload_file_invoice = $_FILES['upload_invoice']['name'];
$config['upload_path'] = './assets/img/uploads/dp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '10240';
$this->load->library('upload', $config);
$upload_dp1 = $this->upload->do_upload('upload_dp1');
$file1 = $this->upload->data('file_name');
$upload_dp2 = $this->upload->do_upload('upload_dp2');
$file2 = $this->upload->data('file_name');
$upload_dp3 = $this->upload->do_upload('upload_dp3');
$file3 = $this->upload->data('file_name');
$upload_invoice = $this->upload->do_upload('upload_invoice');
$file4 = $this->upload->data('file_name');
$this->load->library('upload', $config);
if (!empty($upload_image_dp1) && !empty($upload_image_dp2) && !empty($upload_image_dp3) && !empty($upload_file_invoice))
{
$uploaddata = array(
'upload_dp1' => $file1,
'upload_dp2' => $file2,
'upload_dp3' => $file3,
'upload_invoice' => $file4
);
$this->User_model->update(1,$uploaddata);
$this->session->set_flashdata('success', 'Successfully Insert Your Resume We Will Contact Shortly');
}
else
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
}
===> MODEL
public function update($id,$data)
{
$this->db->where('id','1');
$this->db->update('testingupload',$data);
}
Please Use your Where condition
I've a file uploader. I do the overwrite through the data. It makes me upload these data:
- Pict1.jpg
- Pict12.jpg
- Pict13.jpg
But when I tried to return it, its only showing pict1.jpg. Here the code
Controller:
function send(){
$mariupload = $this->upload_gambar($_FILES['gambar']);
if ($mariupload === FALSE) {
$upload = false;
}
if($upload = false){
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
} else {
$dataupload = $mariupload;
echo $dataupload;
}
private function upload_gambar($files){
//config buat upload gambar
$config['upload_path'] = 'path to upload';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '1024';
$config['overwrite'] = '0';
$this->load->library('upload', $config);
$gambar = array();
if (is_array($files['name']) || is_object($files['name'])){
foreach ($files['name'] as $key => $gambar) {
$_FILES['gambar[]']['name']= $files['name'][$key];
$_FILES['gambar[]']['type']= $files['type'][$key];
$_FILES['gambar[]']['tmp_name']= $files['tmp_name'][$key];
$_FILES['gambar[]']['error']= $files['error'][$key];
$_FILES['gambar[]']['size']= $files['size'][$key];
$fileName = $gambar;
$images[] = $fileName;
$config['file_name'] = $fileName;
$this->upload->initialize($config);
if ($this->upload->do_upload('gambar[]')) {
$dataupload = $this->upload->data('file_name');
return $dataupload.'<br>';
} else {
$mariupload = false;
}
}
}
}
When i try to echo $dataupload in upload_gambar function, it works as an array. But its returning as a single data to send() function. Any idea?
Because you are sending just single file
$mariupload = $this->upload_gambar($_FILES['gambar']);
change this to
$mariupload = $this->upload_gambar($_FILES);
then in your upload_gambar handle it as foreach
You have a return inside the loop. A return will interrupt the loop as the function will not continue to be executed. Try to feed up an array instead.
Replace
return $dataupload.'<br>';
With
$return[] = $dataupload;
And at the end of the function append:
return $return;
I finally found the solution. Thanks to Jovi Wang on Facebook (https://www.facebook.com/jovi.wang.96?fref=ufi) . This is the clean code:
function send(){
$try = $this->upload_multiple($_FILES['gambar']);
if(!$try = false){
$anu= $this->upload_multiple($_FILES['gambar']);
foreach($anu as $key => $anu2){
echo $anu2.'<br>';
}
}
public function upload_multiple($files)
{
// echo '<pre>';
// print_r($files);
// echo '</pre>';
$config['upload_path'] = 'path to upload';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024;
$config['overwrite'] = 0;
$config['encrypt_name'] = TRUE;
$this->load->library('upload');
$this->upload->initialize($config);
$data = array();
foreach($files['name'] as $key => $value)
{
if ( ! empty($files['name'][$key]))
{
$_FILES['gambar_single']['name'] = $files['name'][$key];
$_FILES['gambar_single']['name'] = $files['name'][$key];
$_FILES['gambar_single']['type'] = $files['type'][$key];
$_FILES['gambar_single']['tmp_name'] = $files['tmp_name'][$key];
$_FILES['gambar_single']['error'] = $files['error'][$key];
$_FILES['gambar_single']['size'] = $files['size'][$key];
if ($this->upload->do_upload('gambar_single'))
{
$data[] = $this->upload->data('file_name');
}
else
{
$data[] = $this->upload->display_errors();
}
}
else
{
$data[] = 'empty upload';
}
}
return $data;
}
I am trying to upload an image.
Here is the code
<?php
$PerpetualCalendar = $this->input->post();
if($files['imgQuarter1']['name'][$key]!="")
{
$_FILES['imgQuarter1']['name']= $files['imgQuarter1']['name'][$key];
$_FILES['imgQuarter1']['type']= $files['imgQuarter1']['type'][$key];
$_FILES['imgQuarter1']['tmp_name']= $files['imgQuarter1']['tmp_name'][$key];
$_FILES['imgQuarter1']['error']= $files['imgQuarter1']['error'][$key];
$_FILES['imgQuarter1']['size']= $files['imgQuarter1']['size'][$key];
if(isset($PerpetualCalendar['id1'][$key]))
{
$img_id=$PerpetualCalendar['id1'][$key];
}
else
{
$img_id=$GetLastID;
}
$fileName = $img_id.'_NAME_'. $files['imgQuarter1']['name'][$key];
$config['file_name'] = $fileName;
$this->upload->initialize($config);
if($this->upload->do_upload('imgQuarter1'))
{
if(isset($PerpetualCalendar['NAMEpath_idQ1'][$key])){
$ImgUpdate[$imgkey]['tbl_name']='perpetual_calendar';
$ImgUpdate[$imgkey]['field_name']='event_name';
$ImgUpdate[$imgkey]['user_FK']=$user_id;
$ImgUpdate[$imgkey]['path']=$uploadpath.'/'.$fileName;
$ImgUpdate[$imgkey]['entry_id']=$img_id;
$ImgUpdate[$imgkey]['path_id']=$PerpetualCalendar['NAMEpath_idQ1'][$key]; //for update purpose
}else{
$ImgInsert[$imgkey]['tbl_name']='perpetual_calendar';
$ImgInsert[$imgkey]['field_name']='event_name';
$ImgInsert[$imgkey]['user_FK']=$user_id;
$ImgInsert[$imgkey]['path']=$uploadpath.'/'.$fileName;
$ImgInsert[$imgkey]['entry_id']=$img_id;
}
$imgkey++;
}
else
{
$this->data['error'] = $this->session->set_flashdata(array('error' => $this->upload->display_errors()));
}
}
?>
here is the View
echo form_upload(array('name'=>'imgQuarter1['.$key.']','class'=>'default'),'');
echo form_input(array('name' => 'id1['.$key.']', 'type'=>'hidden','value'=>$ValuePerpetualCalendar['cal_id']));
in above code,i can get $PerpetualCalendar['id1'][$key] this Id,but
image can not upload in the folder and can't save in Database Aswell !
Any Help?
public function upload_multiple_image($user_id){
$image = $_FILES['image']['name'];
$files = $_FILES['image'];
$image_title = $this->input->post('image_title');
$image_link = $this->input->post('image_link');
foreach($image as $key=>$data){
if($files['name'][$key]!=""){
$add_data['user_id'] = $user_id;
$add_data['title'] = $image_title[$key];
$add_data['description'] = $image_link[$key];
$add_data['status'] = 1;
$this->db->insert('user_images',$add_data);
$last_inserted_id = $this->db->insert_id();
$_FILES['image']['name'] = $files['name'][$key];
$_FILES['image']['type'] = $files['type'][$key];
$_FILES['image']['tmp_name'] = $files['tmp_name'][$key];
$_FILES['image']['error'] = $files['error'][$key];
$_FILES['image']['size'] = $files['size'][$key];
$config['upload_path'] = './user_images/original/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['overwrite'] = TRUE;
$config['file_name'] = 'place_image_'.$last_inserted_id;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('image')){
$file_details = $this->upload->data();
$update_data = array('image'=>$file_details['file_name']);
$this->db->update('user_images',$update_data,array('id'=>$last_inserted_id));
}
}
}
}
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'],
I am having trouble resizing photos that I am successfully uploading in my my upload module.
I am able to upload photos to the appropriate folder, however my resizing is not working and I also want to simultaneously create a thumbnail duplicate in the same folder.
public function upload()
{
$this->load->library('session');
$this->load->helper('url');
$session_id = $this->session->userdata('id');
$this->load->model('account_model');
$user = $this->account_model->user();
$data['user'] = $user;
echo $user['id'];
$user_folder = './uploads/' . $this->session->userdata('id');
if(!is_dir($user_folder)){
mkdir($user_folder, 0777);
}
$config['image_library'] = 'gd2';
$config['source_image'] = $user_folder;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 50;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$config['upload_path'] = $user_folder;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '165';
$config['max_width'] = '165';
$config['max_height'] = '165';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = '/account/upload';
$this->load->view('includes/templates/main_page_template', $data);
}
else
{
$data = array('upload_data' => $this->upload->data());
$data['main_content'] = '/account/success';
$this->load->view('includes/templates/main_page_template', $data);
}
}
Try this:
public function upload()
{
$this->load->library('session');
$this->load->helper('url');
$session_id = $this->session->userdata('id');
$this->load->model('account_model');
$user = $this->account_model->user();
$data['user'] = $user;
echo $user['id'];
$user_folder = './uploads/' . $this->session->userdata('id');
if(!is_dir($user_folder)){
mkdir($user_folder, 0777);
}
$this->load->library('image_lib'); #load the image manipulation library without initiatlising it here.
$configThumb['image_library'] = 'gd2';
//$configThumb['source_image'] = $user_folder;
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 50;
$configThumb['height'] = 50;
//$this->image_lib->resize();
$config['upload_path'] = $user_folder;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '165';
$config['max_width'] = '165';
$config['max_height'] = '165';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = '/account/upload';
$this->load->view('includes/templates/main_page_template', $data);
}
else
{
$configThumb['source_image'] = $uploadedDetails['full_path'];
$this->image_lib->initialize($configThumb); #initialize the library here
$this->image_lib->resize(); #resize is done here
$data = array('upload_data' => $this->upload->data());
$data['main_content'] = '/account/success';
$this->load->view('includes/templates/main_page_template', $data);
}
}
Full path refers to the source image that is uploaded to your server. See the else condition you will get the idea. Now, the change is that we are loading the library and initializing it after uploading the image and hence we are getting the full path from $this->upload->data().
EDIT : MY WORKING FUNCTION TO CROP AN IMAGE IN TWO DIFFERENT SIZES
function udpate_profile($userId = 0){
$data = array();
//echo "<pre>";print_r($_POST);echo "</pre>";
/* Upload Image */
if($_FILES['image']['name'] != ""){
//echo "<pre>";print_r($_FILES);echo "</pre>";
//echo "enter";die;
/* Check if previous file exists */
$chkRs = $this->db->select('image')->where('id', $this->session->userdata['logged_user']['id'])->get('admins');
//echo $this->db->last_query();die;
if($chkRs->num_rows() > 0){
$chkD = $chkRs->row_array();
if($chkD['image'] != ""){
### delete the previous image ###
$pathActual = './profile_images/';
$pathMedium = './profile_images/medium/';
$pathThumb = './profile_images/thumbs/';
if(file_exists($pathActual.$chkD['image'])){ #delete the actual image
unlink($pathActual.$chkD['image']);
}
if(file_exists($pathMedium.$chkD['image'])){ #delete the medium image
unlink($pathMedium.$chkD['image']);
}
if(file_exists($pathThumb.$chkD['image'])){ #delete the thumb image
unlink($pathThumb.$chkD['image']);
}
### delete the previous image ###
}
}
/* Check if previous file exists */
//print_r($_FILES['image']);die;
$this->load->library('image_lib');
$configUpload['upload_path'] = './profile_images/';
$configUpload['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$configUpload['max_size'] = '0';
$configUpload['max_width'] = '0';
$configUpload['max_height'] = '0';
$configUpload['encrypt_name'] = true;
$this->load->library('upload', $configUpload);
/* size 64*72 for comments */
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['create_thumb'] = TRUE;
$configThumb['new_image'] = './profile_images/thumbs/';
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 64;
$configThumb['height'] = 72;
$configThumb['thumb_marker'] = "";
//$this->load->library('image_lib');
/* size 64*72 for comments */
/* size 167*167 for profile page */
$configThumbMedium = array();
$configThumbMedium['image_library'] = 'gd2';
$configThumbMedium['create_thumb'] = TRUE;
$configThumbMedium['new_image'] = './profile_images/medium/';
$configThumbMedium['maintain_ratio'] = TRUE;
$configThumbMedium['width'] = 167;
$configThumbMedium['height'] = 167;
$configThumbMedium['thumb_marker'] = "";
/* size 167*167 for profile page */
if(!$this->upload->do_upload('image')){
return 0;
}
$uploadedDetails = $this->upload->data();
if($uploadedDetails['is_image'] == 1){
$configThumb['source_image'] = $uploadedDetails['full_path'];
$configThumbMedium['source_image'] = $uploadedDetails['full_path'];
$raw_name = $uploadedDetails['raw_name'];
$file_ext = $uploadedDetails['file_ext'];
$imgname = $raw_name.$file_ext;
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
$this->image_lib->initialize($configThumbMedium);
$this->image_lib->resize();
}
}
//die();
/* Upload Image */
$data = $this->input->post(null);
//echo "<pre>";print_r($data);echo "</pre>";die;
if(isset($imgname) && $imgname != ""){
$data['image'] = $imgname;
}
$this->db->where('id',$this->session->userdata['logged_user']['id'])->update('admins', $data);
return 1;
}
it's likely you need to pass source_image the full path of the image on the server. so if you're trying to use /uploads, then it's likely you'll need to set it as /var/www/html/uploads or whatever the full path is to that folder since the image processing is relative to the server, not the site url