How to upload data to database using codeigniter - php

I want to upload the image to database using codeigniter. It saved to database but the image didn't send to the folder path i've set and only the image_path are saved to the database. Can you tell me what to do or anything I did wrong? Here's my code:
* Controller *
function saveEvent(){
$config = [
'upload_path' => './assets/img',
'allowed_types' => 'gif|png|jpg|jppeg'
];
$this->load->library('upload', $config);
$this->form_validation->set_error_delimiters();
$this->upload->do_upload();
// var_dump('asd'. $this->upload->do_upload());
// if($this->upload->do_upload()){
$datas = $this->input->post('eventimage');
$info = $this->upload->data();
$image_path = "./assets/img/".$info['raw_name'].$info['file_ext'];
$data['cal_events_image'] = $image_path;
$data['cal_events_name'] = $this->input->post('eventname');
unset($data['submit']);
$this->load->model('CalendarModel');
if($this->CalendarModel->eventinsertdata($data)){
echo "success";
}else{
echo " fail";
}
* View *
<form action="<?php echo base_url('index.php/CalendarController/saveEvent');?>" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<table class="table">
<tr>
<td>Event Type</td>
<td><input type="text" name="eventname"></td>
</tr>
<tr>
<td>Image</td>
<td><input type="file" name="eventimage"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submitevent" value="Add Event" class="btn btn-success"></td>
</tr>
</table>
</form>
* Model *
function eventinsertdata($data){
return $this->db->insert('table', $data);
return $this->db->insert('dev_adkt_events_type', $data);
echo 'hahay';
}
Thank you guys in advance. Hope you could help me with this.

I already solve it! here is my answer and it works!
Controller
function saveEvent(){
$config = array(
'upload_path' => './assets/img',
'allowed_types' => 'gif|png|jpg|jppeg'
);
get_instance()->load->library('upload', $this->config);
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->form_validation->set_error_delimiters();
if($this->upload->do_upload('eventimage')){
$data = $this->input->post('eventimage');
$info = $this->upload->data();
// var_dump($info);
$image_path = "/assets/img/".$info['raw_name'].$info['file_ext'];
$datas['cal_events_image'] = $image_path;
$datas['cal_events_name'] = $this->input->post('eventname');
unset($data['submit']);
$this->load->model('CalendarModel');
if($this->CalendarModel->eventinsertdata($datas)){
// echo "success";
redirect($_SERVER['HTTP_REFERER']);
}else{
echo $this->upload->display_errors();
}
}else{
// echo 'shit';
echo $this->upload->display_errors();
}
}

Related

Multiple File Upload Controls not working in CI

In CodeIgniter, I have 4 file upload controls as under:
<?php echo form_open_multipart('uploadimg');?>
<input type="file" name="image1" />
<input type="file" name="image2" />
<input type="file" name="image3" />
<input type="file" name="image4" />
<input type="submit" value="upload" />
<?php echo form_close();?>
Following is my file upload code:
$name1="img1.jpg";
$config1=array(
'upload_path'=>$_SERVER['DOCUMENT_ROOT']."/c/uploads/",
'allowed_types'=>"gif|jpg|png|jpeg|pdf|JPG|JPEG",
'overwrite'=>TRUE,
'max_size'=>"500",
'file_name'=>$name1,
);
$this->load->library('upload',$config1);
if($this->upload->do_upload('image1'))
{
echo "Image 1 has been uploaded successfully";
}
else
{
echo $this->upload->display_errors();
}
$name2="img2.jpg";
$config2=array(
'upload_path'=>$_SERVER['DOCUMENT_ROOT']."/c/uploads/",
'allowed_types'=>"gif|jpg|png|jpeg|pdf|JPG|JPEG",
'overwrite'=>TRUE,
'max_size'=>"500",
'file_name'=>$name2,
);
$this->load->library('upload',$config2);
if($this->upload->do_upload('image2'))
{
echo "Image 2 has been uploaded successfully";
}
else
{
echo $this->upload->display_errors();
}
$name3="img3.jpg";
$config3=array(
'upload_path'=>$_SERVER['DOCUMENT_ROOT']."/c/uploads/",
'allowed_types'=>"gif|jpg|png|jpeg|pdf|JPG|JPEG",
'overwrite'=>TRUE,
'max_size'=>"500",
'file_name'=>$name3,
);
$this->load->library('upload',$config3);
if($this->upload->do_upload('image3'))
{
echo "Image 3 has been uploaded successfully";
}
else
{
echo $this->upload->display_errors();
}
$name4="img4.jpg";
$config4=array(
'upload_path'=>$_SERVER['DOCUMENT_ROOT']."/c/uploads/",
'allowed_types'=>"gif|jpg|png|jpeg|pdf|JPG|JPEG",
'overwrite'=>TRUE,
'max_size'=>"500",
'file_name'=>$name4,
);
$this->load->library('upload',$config4);
if($this->upload->do_upload('image4'))
{
echo "Image 4 has been uploaded successfully";
}
else
{
echo $this->upload->display_errors();
}
I am getting the following output:
Image 1 has been uploaded successfullyImage 2 has been uploaded
successfullyImage 3 has been uploaded successfullyImage 4 has been
uploaded successfully
But, in the uploads folder, I am getting only 1 file. Strange thing is that the file that is being shown is of last file upload control, but the name of file is img1., i.e. name of first uploaded file.
Why is this happening? What is the solution?
Set each config using the initialize() method and not when loading the upload library.
Changed:
$this->load->library('upload', $config*);
To:
$this->upload->initialize($config*);
Updated code:
$this->load->library('upload');
$name1 = "img1.jpg";
$config1 = array(
'upload_path' => FCPATH . "uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|JPG|JPEG",
'overwrite' => TRUE,
'max_size' => "500",
'file_name' => $name1
);
$this->upload->initialize($config1);
if ($this->upload->do_upload('image1')) {
echo "Image 1 has been uploaded successfully";
} else {
echo $this->upload->display_errors();
}
$name2 = "img2.jpg";
$config2 = array(
'upload_path' => FCPATH . "uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|JPG|JPEG",
'overwrite' => TRUE,
'max_size' => "500",
'file_name' => $name2
);
$this->upload->initialize($config2);
if ($this->upload->do_upload('image2')) {
echo "Image 2 has been uploaded successfully";
} else {
echo $this->upload->display_errors();
}
$name3 = "img3.jpg";
$config3 = array(
'upload_path' => FCPATH . "uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|JPG|JPEG",
'overwrite' => TRUE,
'max_size' => "500",
'file_name' => $name3
);
$this->upload->initialize($config3);
if ($this->upload->do_upload('image3')) {
echo "Image 3 has been uploaded successfully";
} else {
echo $this->upload->display_errors();
}
$name4 = "img4.jpg";
$config4 = array(
'upload_path' => FCPATH . "uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|JPG|JPEG",
'overwrite' => TRUE,
'max_size' => "500",
'file_name' => $name4
);
$this->upload->initialize($config4);
if ($this->upload->do_upload('image4')) {
echo "Image 4 has been uploaded successfully";
} else {
echo $this->upload->display_errors();
}
You may try this. This is working properly to upload two file
(audio, image) or more file to the database.
Table Name : products
| id | product_name | price | description| product_image | audio_file |
HTML
<form name="addProduct" id="addProduct" method="POST" enctype="multipart/form-data">
<input type="text" name="product_name" placeholder="Product Name">
<input type="text" name="price" placeholder="Product Price">
<textarea id="description" name="description" class="materialize-textarea" placeholder="Description"></textarea>
<input type="file" name="product_image" id="fileImg">
<input type="file" name="audio_file" id="audio_file">
<input type="button" name="addProduct" value="Add Product" class="btn btn-flat btn-add-product">
</form>
Controller
public function addProducts(){
/*Initialization of model*/
$this->load->model("product_model");
/*store data into array*/
$result=array(
"product_name"=>$_POST["product_name"],
"price"=>$_POST["price"],
"description"=>$_POST["description"],
);
$proID = $this->product_model->addProduct($result); //sending data to model for insertion
//Define the file names with blog id with same extension which has been uploaded
$product_image = $proID."_product.".pathinfo($_FILES['product_image']['name'], PATHINFO_EXTENSION);
$product_audio = $proID."_audio.".pathinfo($_FILES['audio_file']['name'], PATHINFO_EXTENSION);
$updateData = array(
"product_image" => $product_image,
"audio_file"=>$product_audio
);
// update the name of the images in the database
$this->product_model->updateProduct($updateData,$proID);
//set configuration for the upload library
/* |===> Image Config <==|*/
$config['upload_path'] = 'html/images/products/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = FALSE;
$config['remove_spaces'] = TRUE;
/* |==> Audio Config <==|*/
$config_audio['upload_path'] = 'html/images/products/audios/';
$config_audio['allowed_types'] = 'mp3|mp4';
$config_audio['overwrite'] = TRUE;
$config_audio['encrypt_name'] = FALSE;
$config_audio['remove_spaces'] = TRUE;
//set name in the config file for the feature image
$config['file_name'] = $proID."_product";
$this->load->library('upload', $config); // config first file
$this->upload->do_upload('product_image');
$this->upload->display_errors();
$config_audio['file_name'] = $proID."_audio";
$this->upload->initialize($config_audio); // config seconf file
$this->upload->do_upload('audio_file');
$this->upload->display_errors();
}
Model
/*Add Product*/
public function addProduct($pro_data){
$query=$this->db->insert("products",$pro_data);
$id=$this->db->insert_id();
return $id;
}
/*Update for Multiple File Upload*/
public function updateProduct($result,$up_id){
$this->db->where('id',$up_id);
$this->db->update("products",$result);
}
This is working nicely to my website and i hope it will help you to boost your code.
HTML
<form class="col s12" id='propertyform' enctype="multipart/form-data">
<input type="file" name="image1" />
<input type="file" name="image2" />
<input type="file" name="image3" />
<input type="file" name="image4" />
<a class="waves-effect waves-light btn addproperty">add</a>
</form>
js
$(".addproperty").on("click",function()
{
var baseurl = $("#base_url").val();
var property = new FormData($("#propertyform")[0]);
$.ajax({
url : baseurl+"dropdowns/add_propertyy",
data : property,
type:"POST",
contentType:false,
processData:false,
success:function(res)
{
window.location.reload();
}
});
});
controller
public function add_propertyy()
{
$id = 1;
$images = $id."_image1.".pathinfo($_FILES['image1']['name'], PATHINFO_EXTENSION);
$images1 = $id."_image2.".pathinfo($_FILES['image2']['name'], PATHINFO_EXTENSION);
$images2 = $id."_image3.".pathinfo($_FILES['images3']['name'], PATHINFO_EXTENSION);
$images3 = $id."_image4.".pathinfo($_FILES['image4']['name'], PATHINFO_EXTENSION);
$addimg = array(
"images1" => $images,
"images2" => $images1,
"images3" => $images2,
"images4" => $images3,
);
$this->load->model('property_model');
$this->property_model->add_pro_img($addimg,$id);
$uploadPath = "./html/images/property";
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'gif|jpg|png|pdf';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = FALSE;
$config['remove_spaces'] = TRUE;
$config['file_name'] = $id."_images1";
$this->load->library('upload', $config);
$this->upload->do_upload('images');
$this->upload->display_errors();
$config['file_name'] = $id."_images2";
$this->upload->initialize($config);
$this->upload->do_upload('images2');
$this->upload->display_errors();
$config['file_name'] = $id."_images3";
$this->upload->initialize($config);
$this->upload->do_upload('images3');
$this->upload->display_errors();
$config['file_name'] = $id."_images4";
$this->upload->initialize($config);
$this->upload->do_upload('images4');
$this->upload->display_errors();
}
modal
public function add_pro($data) {
$this->db->insert("property",$data);
$id = $this->db->insert_id();
return $id;
}

filename return empty codeigniter

i have form to upload images in php, when i save the path and name, the name returns empty field in table database here is my code and the value in my database always empty field, please help me
Registration controller
<?php
/**
*
*/
class Registration extends CI_Controller {
function __construct() {
parent::__construct();
//$this->load->library('upload');
$this->load->helper('form');
}
function index() {
$this->load->view('/registration/daftar');
}
function add_user() {
$this->load->model('/daftar/Daftar_Model');
$image_path = APPPATH. 'user_images/';
$config['upload_path'] = $image_path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '500';
$config['max_height'] = '450';
$config['max_width'] = '450';
//$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$path = $this->upload->data();
$image_name1 = $path['file_name'];
$image_path1 = $path['file_path'];
//$this->upload->do_upload('foto');
//$photo_data = $this->upload->data();
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'email' => $this->input->post('email'),
'no_telepon' => $this->input->post('no_telepon'),
'alamat' => $this->input->post('alamat'),
'image_path' => $image_path1,
'image_name' => $image_name1
);
$this->db->select('username');
$this->db->where('username', $data['username']);
$result = $this->db->get('user');
if($result->num_rows() > 0) {
echo "Udah ada";
$this->output->set_header('refresh:2; url='.site_url("/registration"));
} else {
if(! $this->upload->do_upload('foto')) {
var_dump($this->upload->display_errors());
//$this->load->view('/registration/daftar', $error);
} else {
//$data1 = array('upload_data' => $this->upload->data());
$this->Daftar_Model->insert($data);
echo "Registrasi Berhasil";
$this->output->set_header('refresh:2; url='.site_url("/login"));
//$this->load->view('/registration/daftar', $data1);
}
}
}
}
this is my form
<html>
<head>
<title>Daftar</title>
<body>
<?php
?>
<h1><center>Daftar</center></h1>
<?php echo form_open_multipart('registration/registration/add_user'); ?>
<center>
Username <br>
<input type="text" name="username"> <br>
Password <br>
<input type="password" name="password"> <br>
Email <br>
<input type="text" name="email"> <br>
No Telepon <br>
<input type="text" name="no_telepon"> <br>
Alamat <br>
<textarea name="alamat" rows="6" cols="23"></textarea> <br>
</form>
Foto
<input type="file" name="foto"><br><br><br>
<input type="submit" name="submit" value="Daftar">
</center>
</form>
</body>
</head>
</html>
Are you check your function add_user()?
I suggested you to check upload status before you can add to database. If your upload code error, they will show you error and then you will know where to fix it. It should be:
if(!$this->upload->do_upload('foto'))
{
// Print the upload error
var_dump($this->upload->display_errors());
}else{
$return = $this->upload->data();
$image_name = $return['file_name'];
// your code add to database
}
Hope this help.

Customizing the error for file upload in codeingiter

I have a from with two file uploads: one for image upload and another for video uploading. Now the problem is that, if I upload invalid file types for either video or image, it is showing the same error
The filetype you are attempting to upload is not allowed.
What I need is to customize the error so that for video it will be like
The video you are attempting to upload is not allowed.
How can I make this possible? I am sharing the section of code for video and image.
View Page Code:
<tr>
<td>
<label for="video_type">Video Genre <span class="required">*</span></label>
</td>
<td>
<?php
$array['']="Select";
foreach($genre as $row)
{
$array[$row->id] = $row->genre;
}
echo form_dropdown('video_type',$array, set_value('video_type'));
?>
</td>
<td>
<?php echo form_error('video_type'); ?>
</td>
</tr>
<tr>
<td>
<label for="uploader_id">Upload Video <span class="required">*</span></label>
</td>
<td>
<input type="file" name="video" class="input" value="<?php echo set_value('video'); ?>" />
</td>
<td>
<?php echo form_error('video'); ?>
</td>
</tr>
Controller:
function index()
{
$data['dropdown'] = lang_dropdown(); // multilanguage dropdown list
$data['language']=$this->session->userdata('site_language');
$userid=$this->tank_auth->get_user_id();
if (empty($_FILES['thumb_image']['name']))
{
$this->form_validation->set_rules('thumb_image', 'Thumb Image', 'required|max_length[255]');
}
if (empty($_FILES['video']['name']))
{
$this->form_validation->set_rules('video', 'Video', 'required');
}
$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
$data['page']='video/upload';
if ($this->form_validation->run() == FALSE) // validation hasn't been passed
{
$this->load->view('layout/template',$data);
}
else // passed validation proceed to post success logic
{
$file=$_FILES['video']['name'];
$thumbfile=$_FILES['thumb_image']['name'];
$form_data = array(
'videolink' => time().$file,
'videothumbnail' => time().$thumbfile,
'uploaderid' => $userid,
'uploaderid' => set_value('1'),
);
$config['upload_path'] = './secure/';
$config['allowed_types'] = 'mpeg|mp4|mpg|mpe|qt|mov|avi|movie|wmv|flv|3gp|mkv|dv|m4u|m4v|mxu|rv';
$extn = end(explode(".", $_FILES['video']['name']));
$config['file_name'] = time().$_FILES['video']['name'].'.'.$extn;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$data['upload_data'] = '';
if (!$this->upload->do_upload('video'))
{
$data['msg'] = $this->upload->display_errors();
$this->load->view('layout/template',$data);
}
else
{
$data['upload_data'] = $this->upload->data();
if($_FILES['thumb_image']['name']!="")
{
$config1['upload_path'] = './secure/';
$ext = end(explode(".", $_FILES['thumb_image']['name']));
$config1['file_name'] = time().$_FILES['thumb_image']['name'].'.'.$ext;
$config1['allowed_types'] = 'jpg|png|jpeg|gif|bmp|jpe|tiff|tif';
$this->load->library('upload', $config1);
$this->upload->initialize($config1);
if (!$this->upload->do_upload('thumb_image'))
{
$data['msg'] = $this->upload->display_errors();
$this->load->view('layout/template',$data);
}
}
if ($this->Video_model->SaveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db
{
$data['successmsg']="Successfully uploaded the video";
}
else
{
echo 'An error occurred saving your information. Please try again later';
}
$this->form_validation->unset_field_data();
$this->load->view('layout/template',$data);
}
}
}
try this
if (empty($_FILES['video']['name']))
{
$this->form_validation->set_message('required', 'video name incorrect'); //put this line above this statement
$this->form_validation->set_rules('video', 'Video', 'required');
}

Insert each “checked” check box values from multiple checkboxes to the database using codeigniter

i am working in codeigniter php. i want to insert multiple rows for each checked checkbox value with oyhers value. t tried to do it. but it shows me database array error.
In view :
Category Name:
<?php
foreach($result as $aresult)
{
?>
<input type="checkbox" name="category_name[]" value="<?php echo $aresult->category_name;?>" /> <?php echo $aresult->category_name;?> <br>
<?php
}
foreach($area as $aresult1)
{
?>
<input type="checkbox" name="category_name[]" value="<?php echo $aresult1->category_name;?>" /> <?php echo $aresult1->category_name;?> <br>
<?php
}
?>
<tr>
<td>Content Headline:</td>
<td>
<input type="text" required="1" name="content_headline" tabindex="3" placeholder="Content Headline" size="80"/>
</td>
</tr>
<tr>
<td>Picture</td>
<td>
<input type="file" name="image" tabindex="8"/>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="btn" value="Save" tabindex="9"/></td>
</tr>
Controller:
public function savecontent()
{
date_default_timezone_set('Asia/Dhaka');
foreach($this->input->post('category_name') as $rm){
$data=array(
$now = date("Y-m-d H:i:s"),
$data['admin_id']=$this->session->userdata('admin_id'),
$data['category_name']=$this->input->post('category_name',true),
$data['content_headline']=$this->input->post('content_headline',true),
$this->load->library('upload');
$config['upload_path'] = './images/news_images/';
$config['allowed_types'] = 'gif|jpg|png|mp3';
$config['max_size'] = '100000';
$config['max_width'] = '1024';
$config['max_height'] = '720';
$error = '';
$udata = '';
$udata1 = '';
$udata2 = '';
$udata3 = '';
$this->upload->initialize($config);
if (!$this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors());
}
else {
$udata = array('upload_data' => $this->upload->data());
$data['image'] = "images/news_images/" . $udata['upload_data']['file_name'];
}
);
}
$this->co_model->save_content($data);
}
Model:
public function save_content($data)
{
$this->db->insert('content',$data);
}
this code shows me database array error. so now how i solve this problem?
Move $this->co_model->save_content($data); inside foreach loop.
Try this code
public function savecontent()
{
date_default_timezone_set('Asia/Dhaka');
foreach($this->input->post('category_name') as $rm)
{
$data=array();
$now = date("Y-m-d H:i:s"),
$data['admin_id']=$this->session->userdata('admin_id'),
$data['category_name']=$this->input->post('category_name',true),
$data['content_headline']=$this->input->post('content_headline',true),
$this->load->library('upload');
$config['upload_path'] = './images/news_images/';
$config['allowed_types'] = 'gif|jpg|png|mp3';
$config['max_size'] = '100000';
$config['max_width'] = '1024';
$config['max_height'] = '720';
$error = '';
$udata = '';
$udata1 = '';
$udata2 = '';
$udata3 = '';
$this->upload->initialize($config);
if (!$this->upload->do_upload('image'))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$udata = array('upload_data' => $this->upload->data());
$data['image'] = "images/news_images/" . $udata['upload_data']['file_name'];
}
$this->co_model->save_content($data);
}
}

Codeigniter - How to add Images to my CRUD methods?

I am working on my second CI application. I have created some simple CRUD methods in my controller. My client has requested that each record includes an image. I have searched the forums and other resources for help but haven’t had much luck.
I have managed to upload files into a directory using the File Uploading Class, my problem though is how to associate the uploaded file/s with the relevant record.
Here are the relevant parts of my MVC.., any help/point in the right direction would be appreciated.
View - admin/locationEdit.php
<form method="post" action="<?php echo $action; ?>">
<div class="data">
<table>
<tr>
<td width="30%">ID</td>
<td><input type="text" name="id" disabled="disable" class="text" value="<?php echo $this->validation->id; ?>"/></td>
<input type="hidden" name="id" value="<?php echo $this->validation->id; ?>"/>
</tr>
<tr>
<td valign="top">Name<span style="color:red;">*</span></td>
<td><input type="text" name="name" class="text" value="<?php echo $this->validation->name; ?>"/>
<?php echo $this->validation->name_error; ?></td>
</tr>
<tr>
<td valign="top">Address<span style="color:red;">*</span></td>
<td><input type="text" name="address" class="text" value="<?php echo $this->validation->address; ?>"/>
<?php echo $this->validation->address_error; ?></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save"/></td>
</tr>
</table>
</div>
</form>
Controller - location.php
function add(){
// set validation properties
$this->_set_fields();
// set common properties
$data['title'] = 'Add new location';
$data['message'] = '';
$data['action'] = site_url('admin/location/addLocation');
$data['link_back'] = anchor('admin/location/index/','Back to list of locations',array('class'=>'back'));
// Write to $title
$this->template->write('title', 'Admin - Add New Location!');
// Write to $sidebar
$this->template->write_view('content', 'admin/locationEdit', $data);
// Render the template
$this->template->render();
}
function addLocation(){
// set common properties
$data['title'] = 'Add new location';
$data['action'] = site_url('admin/location/addLocation');
$data['link_back'] = anchor('admin/location/index/','Back to list of locations',array('class'=>'back'));
// set validation properties
$this->_set_fields();
$this->_set_rules();
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$path_to_uploads='./uploads';
$config['upload_path'] = $path_to_uploads;
$this->load->library('upload', $config);
$upload_data=$this->upload->data();
$file_name=$upload_data['file_name'];
$full_file_path = $path_to_uploads.'/'.$file_name;
// run validation
if ($this->validation->run() == FALSE){
$data['message'] = '';
}else{
// save data
$location = array('name' => $this->input->post('name'),
'address' => $this->input->post('address'),
'image_url' => $full_file_path);
$id = $this->locationModel->save($location);
// set form input name="id"
$this->validation->id = $id;
// set user message
$data['message'] = '<div class="success">add new location success</div>';
}
// Write to $title
$this->template->write('title', 'Admin - Add New Location!');
// Write to $sidebar
$this->template->write_view('content', 'admin/locationEdit', $data);
// Render the template
$this->template->render();
}
Within your upload function, get the path of the uploaded file:
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$path_to_uploads='./uploads';
$config['upload_path'] = $path_to_uploads;
$this->load->library('upload', $config);
//add this
$this->upload->initialize($config);
if (!$this->upload->do_upload()){
$error = $this->upload->display_errors();
echo "<script>alert($error);</script>";
}else{
$upload_data=$this->upload->data();
$file_name=$upload_data['file_name'];
$full_file_path = $path_to_uploads.'/'.$file_name;
}
then you can return $full_file_path back to the method that called it and insert it into the db, or just insert directly.

Categories