I do not understand how to fix this codes:
return $this->db->update('slideshow');
I have tried several codes like changing it to $query or $query_result etc. and the error still exist. I still have to use $this->db->update('');
controllers/Cuploadfile.php
function uploadedit()
{
$data['images'] = ''; // Need to place it here and get images here.
//set preferences
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|gif';
$config['max_size'] = '500000';
//load upload class library
$this->load->library('upload', $config);
$this->load->model('Mpages');
if (!$this->upload->do_upload('filename'))
{
// case - failure
$upload_error = array('error' => $this->upload->display_errors());
$this->load->view('addslideshows', $upload_error);
}
else
{
// case - success
$upload_data = $this->upload->data();
$filename = $upload_data['file_name'];
$image_id = $this->uri->segment(3);
$data['images'] = $this->Mpages->edit_slideshow($filename, $image_id);
$data['success_msg'] = '<div class="alert alert-success text-center">Your file <strong>' . $upload_data['file_name'] . '</strong> was successfully uploaded!</div>';
$this->load->view('editslideshows', $data);
}
}
models/Mpages.php
public function edit_slideshow($filename, $image_id)
{
$this->db->set('slideshow_image', $filename);
$this->db->where('image_id', $image_id);
return $this->db->update('slideshow');
}
Change your Model's function to this
public function edit_slideshow($filename, $image_id){
$this->db->where('image_id', $image_id);
$this->db->update('slideshow', $filename);
if($this->db->affected_rows() > 0){
return true;
}else{
return false;
}
}
rewrite your model
public function edit_slideshow($filename, $image_id)
{
$this->db->set('slideshow_image', $filename);
$this->db->where('image_id', $image_id);
$update=$this->db->update('slideshow');
if($update)
{
return true;
}
return false;
}
Related
i try to upload image and insert it into database but it's not working.
there is no any error showing in page and image is also not uploading or inserting into database,
please, give me a solution for that how can i solved this problem?
my code is here,
the code of controller is here, enter code here
public function add_product()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$input_name = "product_image";
if (!$this->upload->do_upload($input_name))
{
$data['items'] = $this->data_model->get_data_info('product_id','product');
$this->load->view('product',$data);
}
else
{
$image_info = $this->upload->data();
$data = array(
'product_name'=>$this->input->post('product'),
'product_pic'=>$image_info['file_name'],
'description'=>$this->input->post('description')
);
$query = $this->data_model->add_data($data,'product');
if($query == TRUE){
echo "product added";
$data['items'] = $this->data_model->get_data_info('product_id','product');
$this->load->view('product',$data);
}else{
echo"product already exists";
$data['items'] = $this->data_model->get_data_info('product_id','category');
$this->load->view('product',$data);
}
}
}
model :
public function add_data($data,$table){
$result = $this->db->get_where($table,$data);
$query = (bool)$result->num_rows();
if(!$query){
if($this->db->insert($table,$data)){
return TRUE;
}else{
return FALSE;
}
}else{
return FALSE;
}
}
controller code
function add_product() {
if (!is_dir('./Uploads/')) {
mkdir('./Uploads/', 0777, TRUE);
}
if (!empty($_FILES['product_image'])) {
$config['upload_path'] = './Uploads/';
$config['allowed_types'] = '*';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$config['file_name'] = date('U') . '_' . $_FILES['product_image']['name'];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('product_image')) {
$error = array('error' => $this->upload->display_errors());
print_r($error);
die;
} else {
if ($this->upload->do_upload('product_image')) {
$image_data = $this->upload->data();
$full_path = $config['file_name'];
$data["product_pic"] = $full_path;
$query = $this->data_model->add_data($data, 'product');
if ($res == TRUE) {
echo "product added";
$data['items'] = $this->data_model->get_data_info('product_id', 'product');
$this->load->view('product', $data);
} else {
echo"product already exists";
$data['items'] = $this->data_model->get_data_info('product_id', 'category');
$this->load->view('product', $data);
}
}
}
}
$this->load->view('product');
}
model code
function add_data($data, $table) {
if ($query = $this->db->insert($table,$data)) {
$insert_id = $this->db->insert_id();
return $insert_id;
} else {
return false;
}
}
I want to delete the image long after I update the new image. I have added unlink(), but the old image is not deleted.
Controller :
public function Update()
{
$id = $this->input->post('id');
$name = $this->input->post('name');
if($_FILES['image']['name']!="")
{
$config['upload_path'] = './image/';
$config['allowed_types'] ='gif|jpg|png|jpeg|jpe|pdf|doc|docx|rtf|text|txt';
$this->load->library('upload', $config);
if($this->upload->do_upload('image')){
$uploadData = $this->upload->data();
$image = $uploadData['file_name'];
}else{
$image= '';
}
}else{
$image = '';
}
$data = array(
'name' => $name,
);
if($image != ''){
$data['image'] = $image;
unlink("./image/$row->file_name");
}
$this->model_user->update_user($data,$id);
}
Model :
public function update_user($data, $id)
{
$this->db->where('id', $id);
return $this->db->update('table_user', $data);
}
In your code, you are not getting old image name from DB
First, get the old image name and then try like this
if($image != ''){
$data['image'] = $image;
$oldimage = $this->model_user->get_oldimage($id);
unlink("path/to/directory/".$oldimage);
}
Model
public function get_oldimage($id){
return $this->db->get_where('table_user', ['id' => $id])->row()->image;
}
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 trying to update my information: In the localhost it is not error variable: img, but when i upload it into the server it is error like that A PHP Error was encountered:
Severity: Notice Message: Undefined variable: img Filename: controllers/pages.php Line Number: 79....
function update_ads($id)
{
if($this->input->post('update_ads'))
{
$this->db->select('img');
$this->db->where('id',$id);
$query = $this->db->get('tblp_label_slides',$id);
foreach($query->result() as $row)
{
$img = $row->img;
$image = $this->session->set_userdata('img', $img);
}
if($img!='')
{
$config['upload_path'] ='./images/ads';
$config['allowed_types'] ='jpg|jpeg|gif|png';
$config['max_size'] = 1024*8;
$config['encrypt_name'] = true;
$this->upload->initialize($config);
$this->load->library('upload', $config);
if(!$this->upload->do_upload('imgfile'))
{
$edit['update'] = $this->mod_write->up_adv($this->session->userdata('img'), $id);
$this->session->set_flashdata(array('success_update' => 12));
redirect('pages/ads_right');
}
else
{
$img_content = $this->upload->data('imgfile');
$edit['update'] = $this->mod_write->up_adv($img_content['file_name'], $id);
unlink("./images/ads/".$img);
$this->session->set_flashdata(array('success_update' => 12));
redirect('pages/ads_right');
}
}
else
{
$config['upload_path'] = './images/ads';
$config['allowed_types'] = 'jpg|jpeg|gif|png';
$config['max_size'] = 1024*8;
$config['encrypt_name'] = true;
$this->upload->initialize($config);
$this->load->library('upload', $config);
if(! $this->upload->do_upload('imgfile'))
{
$edit['update'] = $this->mod_write->up_adv('', $id);
$this->session->set_flashdata(array('success_update' => 12));
redirect('pages/ads_right');
}
else
{
$img_content = $this->upload->data('imgfile');
$edit['update'] = $this->mod_write->up_adv($img_content['file_name'], $id);
unlink("./images/ads/".$img);
$this->session->set_flashdata(array('success_update' => 12));
redirect('pages/ads_right');
}
}
}
}
my model
function up_adv($img_content)
{
$id = $this->uri->segment(3);
$data = array(
'dates'=>date('Y-m-d H:i:s'),
'url'=>replace_tag($this->input->post('url')),
'title'=>replace_tag($this->input->post('title')),
'img'=>$img_content,
'type'=>$this->input->post('type')
);
$this->db->where('id',pde($id))
->update('tblp_label_slides',$data);
}
I could be wrong but I think the issue how you are validating $img.
Change if ($img != '')
To if (isset($img) && trim($img) !== '')
This will stop an error being thrown if $img has not been declared which will happen if your query doesn't return any rows.
Hope this helps!
Please help me! I want to upload a file with codeigniter, but this return to error. This is the uploader:
class Uploader extends CI_Model
{
function __construct()
{
parent::__construct();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|exe|xls|doc|docx|xlsx|rar|zip';
$config['max_size'] = '8192'; // kbytes
$config['encrypt_name'] = TRUE;
$this->load->library( 'upload', $config );
$this->response = '';
$this->success = TRUE;
}
/**
* Triggers upload
* returns the file's details on success or false
*/
function upload( $field_name )
{
if ( !$this->upload->do_upload( $field_name ) )
{
$this->success = FALSE;
$this->response = $this->upload->display_errors();
return false;
}
else
{
$this->success = TRUE;
$this->response = $this->upload->data();
return $this->response['file_name'];
}
}
}
And this is the error message:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Downloads::$required_empty
Filename: core/Model.php
Line Number: 51
The autoload database config is on. Please help me.
try this
if($this->input->post())
{
$file_element_name = 'image'; //this is the name of your input file. for example "image"
if ($_FILES['image']['name']!= "")
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|exe|xls|doc|docx|xlsx|rar|zip';
$config['max_size'] = '8192';
$config['remove_spaces']=TRUE; //it will remove all spaces
$config['encrypt_name']=TRUE; //it wil encrypte the original file name
$this->load->library('upload', $config);
if (!$this->upload->do_upload($file_element_name))
{
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error',$error['error']);
redirect('controller_name/function_name','refresh');
}
else
{
$data = $this->upload->data();
return $data['file_name'];
}
$this->session->set_flashdata('msg','success message');
redirect('controller_name/function_name','refresh');
}
else
{
//if no file uploaded the do stuff here
}
}
please let me know if you face any problem.
have you tried something like this
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$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());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
?>
you can get more here http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
The Upload Folder
You'll need a destination folder for your uploaded images. Create a
folder at the root of your CodeIgniter installation called uploads and
set its file permissions to 777.