i want to upload file in codeigniter and want to show error in view file but display error array is not displaying at view file. when uploading file is failed, relevant error is not display at view
function add_model()
{
$this->checkuser();
if(isset($_POST['add_model']))
{
$config['upload_path'] = 'uploads/model/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = rand(1111, 99999).'_'.$_FILES['userfile']['name'];
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$mDate = date('Y-m-d H:i:s');
$this->form_validation->set_rules('model_name', 'Model Name', 'trim|xss_clean|required');
$this->form_validation->set_rules('model_slug', 'Model Slug', 'trim|xss_clean|required');
if($this->form_validation->run() && $this->upload->do_upload())
{
$temp = $this->upload->data('userfile');
$model_image = $temp['file_name'];
$data = array(
'model_name' => $this->input->post('model_name'),
'model_image' => $model_image,
'brand_id' => $this->input->post('brand_id'),
'model_slug' => $this->toAscii($this->input->post('model_slug'))
);
$this->model_model->addmodel($data);
redirect('admin/model/model_listing');
}
else
{
$data['errors'] = array('error' => $this->upload->display_errors());
$data['brands'] = $this->model_model->allbrand();
$data['page_title'] = 'Add Content';
$this->load->view('admin/add_model', $data);
}
}
else
{
$data['errors'] = $this->upload->display_errors();
$data['brands'] = $this->model_model->allbrand();
$data['page_title'] = 'Add Content';
$this->load->view('admin/add_model', $data);
}
}
given below is the code of view file where i want to show error
<?php
if(validation_errors())
{
echo validation_errors('<div class="alert alert-error">', '</div>');
echo '<div class="alert alert-error">'.$error.'</div>';
}
if($errors)
{
foreach ($errors as $error)
{
echo '<div class="alert alert-error">'.$error.'</div>';
}
}
?>
i am using both validation error and display error at the same time
change this if condition
if($this->form_validation->run() && $this->upload->do_upload())
{
to this
if($this->form_validation->run() && $this->upload->do_upload('name'))
{
do_upload function requires name of field to upload image..
Related
How to resize the news picture? I never could.
My problem [source_image]
I use Codeigniter 3.1.6
Controller
public function insert_news() {
$config['upload_path'] = 'uploads/news/';
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if ($this->upload->do_upload('news_image'))
{
$image = $this->upload->data();
$image_url = $image['file_name'];
$db_insert ='upload/news/'.$image_url.'';
$data=array(
'news_title' => $this->input->post('news_title'),
'news_content' => $this->input->post('news_content'),
'news_image' => $db_insert,
'news_sef'=>sef($this->input->post('news_title'))
);
$this->load->model('vt');
$result = $this->vt->insert_news($data);
if ($result) {
echo "yes";
} else {
echo "no";
}
}
}
You can try this solution for your problem :
Controller :
<?php
public function insert_news() {
$config['upload_path'] = 'uploads/news/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '20240000245';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;
// You can change width & height
$imgage_width= 60;
$imgage_height= 60;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($field)) {
$error = $this->upload->display_errors();
// uploading failed. $error will holds the errors.
$this->form_validation->set_message('error',$error);
return FALSE;
} else {
$fdata = $this->upload->data();
$configer = array(
'image_library' => 'gd2',
'source_image' => $fdata['full_path'],
'maintain_ratio' => TRUE,
'width' => $imgage_width,
'height' => $imgage_height,
);
// Load Image Lib Library
$this->load->library('image_lib');
$this->image_lib->clear();
$this->image_lib->initialize($configer);
$this->image_lib->resize();
$img_data ['path'] = $config['upload_path'] . $fdata['file_name'];
// uploading successfull, now do your further actions
$data=array(
'news_title' => $this->input->post('news_title'),
'news_content' => $this->input->post('news_content'),
'news_image' => $img_data ['path'],
'news_sef'=>sef($this->input->post('news_title'))
);
$this->load->model('vt');
$result = $this->vt->insert_news($data);
if ($result) {
$this->form_validation->set_message('success',"News has been added successfully.");
redirect('contorller/action_name');
} else {
$this->form_validation->set_message('error',"Error in aading news");
redirect('contorller/action_name');
}
}
}
?>
I Hope it will Help you.
In my page I want user to select multiple images and upload it I am saving images name in database for reference. I am successful in uploading single images in database and can also show image in view but now I have problem in uploading multiple images.
public function add_record()
{
$this->form_validation->set_rules('category', 'category', 'required');
$current_date = date("Y-m-d H:i:s");
$error='';
if($this->form_validation->run())
{
$image = '';
if($_FILES['image']['name'])
{
if (!is_dir('/backend_assets/media/image/')) {
mkdir('./backend_assets/media/image/', 0777, TRUE);
}
$config['upload_path'] = './backend_assets/media/image/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('image'))
{
$data = $this->upload->data();
$image = $data['file_name'];
}else{
$this->session->set_flashdata('error', $this->upload->display_errors());
redirect(base_url('admin/image'));
}
}
$insert_array = array(
'gl_cat_id' => $this->input->post('category'),
'gl_image'=> $image
);
if ($this->common_model->add_records('vm_image',$insert_array))
{
$id = $this->db->insert_id();
$insert_sco_details = array(
'sd_ty'=>'vm_image',
'sd_ty_id'=>$id,
'sd_image'=>$image
);
if($this->common_model->publication('vm_image',$id) && $this->common_model->add_records('vm_seo_detail',$insert_sco_details))
{
$this->session->set_flashdata('success','Record added successfully');
redirect(base_url('admin/image'));
}else{
$this->session->set_flashdata('error','Error while adding record');
redirect(base_url('admin/image'));
}
}else{
$this->session->set_flashdata('error','Error while adding record');
redirect(base_url('admin/image'));
}
}
$where_array = array('vm_publications.status !=' => 4);
$data['users_type'] = $this->common_model->get_records('vm_image_category','','','');
$data['include'] = 'backend/image/add_image';
$this->load->view('backend/container', $data);
}
How is it possible with above code...?
$current_date = date("Y-m-d H:i:s");
$error = '';
$image = '';
if(isset($_FILES['image']['name']))
{
//print_r($_FILES);
$id = base64_decode($this->input->post('gid'));
$filesCount = count($_FILES['image']['name']);
$inserted = '';
for($i = 0; $i < $filesCount; $i++)
{
$_FILES['userFile']['name'] = $_FILES['image']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['image']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['image']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['image']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['image']['size'][$i];
$config['upload_path'] = './backend_assets/media/image/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('userFile'))
{
$fileData = $this->upload->data();
$image = $fileData['file_name'];
$insert_array = array(
'gl_cat_id' => $this->input->post('category'),
'gl_image'=> $image
);
if ($this->common_model->add_records('vm_image',$insert_array))
{
$id = $this->db->insert_id();
$insert_sco_details = array(
'sd_ty'=>'vm_image',
'sd_ty_id'=>$id,
'sd_image'=>$image
);
if($this->common_model->publication('vm_image',$id) && $this->common_model->add_records('vm_seo_detail',$insert_sco_details))
{
$inserted++;
}
}
}
}
if($inserted == $filesCount)
{ $this->session->set_flashdata('success','Images uploaded successfully');
redirect(base_url('adminp8AamG6ueHFNGAAp/image'));
}else{
$this->session->set_flashdata('error','Error while adding record');
redirect(base_url('adminp8AamG6ueHFNGAAp/image'));
}
}
$where_array = array('vm_publications.status !=' => 4);
$data['users_type'] = $this->common_model->get_records('vm_image_category','','','');
$data['include'] = 'backend/image/add_image';
$this->load->view('backend/container', $data);
try below code in you add_record() function. it will helpful to you. few days ago, i have faced same problem
$files = $_FILES;
$count = count($_FILES['image']['name']);
for($i=0; $i<$count; $i++) {
$_FILES['image']['name']= $files['image']['name'][$i];
$_FILES['image']['type']= $files['image']['type'][$i];
$_FILES['image']['tmp_name']= $files['image']['tmp_name'][$i];
$_FILES['image']['error']= $files['image']['error'][$i];
$_FILES['image']['size']= $files['image']['size'][$i];
$this->upload->initialize($this->set_upload_options());//function defination below
$this->upload->do_upload('image');
$upload_data = $this->upload->data();
$name_array[] = $upload_data['file_name'];
$fileName = $upload_data['file_name'];
$images[] = $fileName;
}
$fileName = $images;
and set file upload configuration in set_upload_options function in same controller.
function set_upload_options() {
$config = array();
$config['upload_path'] = PATH;
$config['remove_spaces']=TRUE;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '78000';
return $config;
}
First, debug the output of the $_FILES variable. This should give you an array of files that are being uploaded. Loop through them to treat each one individually.
foreach ($_FILES as $file) {
// do some file processing on the $file object instead of $_FILES object.
// example: instead of using 'if($_FILES['image']['name'])'
// use: 'if($file['image']['name'])'
}
If you want to use CI's upload class, check out their Docs here: https://www.codeigniter.com/userguide3/libraries/file_uploading.html
I'm new here, so nice to meet you all.
I have trouble with codeigniter.
Maybe hard to tell so I will show all of my code :
my controller :
function upload_barang(){
$this->admin_model->login();
$this->admin_model->valid_product();
$config['upload_path'] = './produk/';
$config['allowed_types'] = 'jpg';
$config['max_size'] = '100';
$config['max_width'] = '400';
$config['max_height'] = '400';
$this->load->library('upload',$config);
$data['query'] = $this->db->get('kategori');
if($this->form_validation->run() == FALSE && ! $this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
$data['success'] = '';
$this->load->view('backoffice/tambahbarang',$data);
} else {
$upload_data = $this->upload->data();
$data['error'] = '';
$data = array(
'idkategori' => $this->input->post('idkategori'),
'namabarang' => $this->input->post('nbarang'),
'image' => $upload_data['file_name'],
'status' => 1
);
$data_insert = $this->db->insert('barang',$data);
if($data_insert == TRUE) {
$data['query'] = $this->db->get('kategori');
$query = $this->db->get('barang');
$rows = $query->row();
$data['idbarang'] = $rows->idbarang;
$harga = array(
'idbarang' => $data['idbarang'],
'satuan' => $this->input->post('stcbarang'),
'harga' => $this->input->post('hbarang')
);
$this->db->insert('hargabarang',$harga);
$data['success'] = '<b>Barang Telah Ditambahkan</b>';
$this->load->view('backoffice/tambahbarang',$data);
} else {
$data['success'] = '<b>Barang Gagal Ditambahkan</b>';
$this->load->view('backoffice/tambahbarang',$data);
}
}
}
This code success insert all of data to database, except on column 'image' that I fill with '$upload_data['file_name']' this empty on my database.
what's wrong with my code?
please help thanks
You can use this format:
/*
* ------- Start Image Upload---------
*/
$config['upload_path'] = 'image/product_images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '400';
$config['max_height'] = '400';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$error='';
$fdata=array();
if ( ! $this->upload->do_upload('product_image'))
{
$error = $this->upload->display_errors();
echo $error;
exit();
}
else
{
$fdata = $this->upload->data();
$data['product_image']=$config['upload_path'] .$fdata['file_name'];
}
/*
* --------End Image Upload---------
*/
$this->super_admin_model->save_product_info($data);
my csv file is locally uploading perfectly but when i upload it on my website then its not uploading following is my upload function
function upload_title()
{
if(isset($_POST['upload_titles'])) // check if submit button is clicked
{
$config['upload_path'] = 'uploads/title/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload();
$temp = $this->upload->data('userfile');
$article_titlelist = $temp['file_name'];
if($this->upload->do_upload())
{
$this->load->library('csvreader');
$result = $this->csvreader->parse_file('./uploads/title/'.$article_titlelist);
foreach($result as $val)
{
$articletitle = $val['title'];
$title_category = $val['category'];
$mDate = date('Y-m-d H:i:s');
$data = array(
'article_title' => $articletitle,
'title_category' => $title_category,
'article_cdate' => $mDate,
'article_mdate' => $mDate
);
$this->article_model->addtitle($data);
}
redirect('admin/article/title_listing');
}
}
$error = array('error' => $this->upload->display_errors());
$data['page_title'] = 'Article Title';
$data['content'] = $this->load->view('admin/add_title', $error, true);
$this->load->view('admin/template', $data);
}
i have check it online its not going in do_upload condition
I want to upload 3 images and create thumb of them and want to store their name in databse .
Any idea what to do.NOte I have read many forum and questions but none has helped me in doing both upload and store in database.Here is what i have done for 1 image,it would help me if i can do it same way for multiple images with little modification
View
<p>
<label>Image 1</label>
<input type="file" name="userfile" value=""/>
// I want same kind of anothe two uploads say image2,image3
</p>
Controller
function insert()
{
$data = array('title'=>'Add New Image',
'link_add'=>site_url('manage/img_gallaryslider/add'),
'edit_link'=>site_url('manage/img_gallaryslider/edit'), 'action'=>site_url('manage/img_gallaryslider/insert'),
'link_back'=>site_url('manage/img_gallaryslider'),
'tbl'=>'imgslider');
$this->_set_fields();
$this->_set_rules();
if ($this->form_validation->run() == TRUE)
{
$config['upload_path'] = './images/imagegallaryslider';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
if($_FILES['userfile']['name']=='')
{
$data['upload_error']='<strong>Error: Please, select image to upload</strong>';
}
else
{
$data['upload_error']='<strong>Error:Invalid file format or size</strong>';
}
$this->load->view('manage/includes/header', $data);
$this->load->view('manage/img_gallaryslideredit', $data);
$this->load->view('manage/includes/footer');
}
else
{
$data = array('upload_data' => $this->upload->data());
$filename= $this->upload->data();
$file_name=$this->upload->file_name;
$this->create_thumb($file_name);
// save data
$this->ip_date = $this->admin_model->get_date_ip();
$value_array = array('Name' => $this->input->post('name'),
'Image'=>$this->upload->file_name,
'CreatedBy' => $this->session->userdata('adminid'),
'CreatedDate'=>$this->ip_date->cur_date,
'CreatedIp'=>$this->ip_date->ip);
$id = $this->admin_model->save('imggallaryslider',$value_array);
$this->session->set_flashdata('notification',$this->lang->line('gen_succ_added'));
redirect(site_url('manage/img_gallaryslider/index'));
die();
}
}
else
{
$this->load->view('manage/includes/header', $data);
$this->load->view('manage/img_gallaryslideredit', $data);
$this->load->view('manage/includes/footer');
}
}
function create_thumb($file_name)
{
$config['image_library'] = 'gd2';
$config['source_image'] = './images/imagegallaryslider/'.$file_name;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 70;
$config['height'] = 50;
$config['new_image'] = './images/imagegallaryslider/thumb/'.$file_name;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
}
model
function save($table,$value)
{
$this->db->insert($table, $value);
return $this->db->insert_id();
}
</code>
The method do_upload() declared as public function do_upload($field = 'userfile'),
so you can use it in your code like:
private function uploads() {
$config['upload_path'] = './images/imagegallaryslider';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
//<--HERE the several images handler
foreach (array('userfile','img1','img2' as $field) {
if ( ! $this->upload->do_upload($field))
{
$error = array('error' => $this->upload->display_errors());
if($_FILES[$field]['name']=='')
{
$data['upload_error']='<strong>Error: Please, select image to upload</strong>';
}
else
{
$data['upload_error']='<strong>Error:Invalid file format or size</strong>';
}
return $data;
}
$data = array('upload_data' => $this->upload->data());
$filename= $this->upload->data();
$file_name=$this->upload->file_name;
$this->create_thumb($file_name);
// save data
$this->ip_date = $this->admin_model->get_date_ip();
$value_array = array('Name' => $this->input->post('name'),
'Image'=>$this->upload->file_name,
'CreatedBy' => $this->session->userdata('adminid'),
'CreatedDate'=>$this->ip_date->cur_date,
'CreatedIp'=>$this->ip_date->ip);
$id = $this->admin_model->save('imggallaryslider',$value_array);
}
$this->session->set_flashdata('notification',$this->lang->line('gen_succ_added'));
redirect(site_url('manage/img_gallaryslider/index'));
die();
}
in view it should look like:
<p>
<label>Image 1</label>
<input type="file" name="userfile" value=""/>
<input type="file" name="img1" value=""/>
<input type="file" name="img2" value=""/>
</p>