This is form's type etc
<?php
echo form_open_multipart("/Register/registerformModel",array("name"=>"subform2","id"=>"subform2"));
?>
input type
<input name="profile" id="profile" type="file" class="input"/>
function
$this->load->library('upload');
$config['upload_path'] = $this->config->item('upload_url_path').'images/member/';
$config['allowed_types'] = 'pdf|doc';
$config['max_size'] = '0';
$config['profile_name'] = date('Ymd_his_').rand(10,99).rand(10,99).rand(10,99);
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('profile'))
{
$data['upload_data']['profile_name'] = '';
echo $this->upload->display_errors('<p style="color:#FF0000;">','</p>');
}
else
{
$data = array('upload_data' => $this->upload->data('profile'));
}
$data['query'] = array(
'profile' => $data['upload_data']['profile_name']
);
$this->session->set_userdata($data['query']);
$num = $this->common_model->insert('member',$data['query']);
PDF/DOC cannot be uploaded with this code? Can anybody help me??
Related
I need to insert multiple values in the checkbox in Codeigniter. how I can write this in the controller.
I tried so many but the values not inserting .
View.Php
<div class="custom-file">
<input type="checkbox" name="size[]" value="Medium"> M
<input type="checkbox" name="size[]" value="Large">L
<input type="checkbox" name="size[]" value="XL"> XL
</div>
Model
public function product_insert($data)
{
// grab user input
$this->db->insert('product', $data);
return $this->db->insert_id();
}
Controller
public function newProduct()
{
$data = array();
$this->load->library('form_validation');
$this->load->library('image_lib');
$this->load->helper('file');
$this->load->helper('string');
// Load the model
$this->load->model('admin/product_model');
$this->load->model('admin/category_model');
$data['category'] = $this->category_model->active_category_listing();
// $data['brand'] = $this->product_model->brand_listing();
$this->form_validation->set_error_delimiters('<div class="alert alert-danger">', '</div>');
// Validating Field
$this->form_validation->set_rules('product_title', 'Title', 'required');
if ($this->form_validation->run() == false) {
$this->load->view('admin/add_product', $data);
}
//insert size checkboxes into database.
$sizeArray = $this->input->post('size'); // Array
$sizeString = implode(",", $sizeArray); // String
else {
$config['upload_path'] = 'uploads/product_images/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 10024;
$this->load->library('upload', $config);
// upload file to directory
$uploadedFile ='';
if ($this->upload->do_upload('product_img')) {
$uploadData = $this->upload->data();
$uploadedFile = $uploadData['file_name'];
$config['image_library'] = 'gd2';
$config['source_image'] = $uploadData['full_path'];
$config['new_image'] = 'uploads/product_images/thumb';
$config['create_thumb'] = false;
$config['maintain_ratio'] = TRUE;
$config['width'] = 311;
$config['height'] = 415;
$this->image_lib->initialize($config);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$data['success_msg'] = 'File has been uploaded successfully.';
} else {
$data['error_msg'] = $this->upload->display_errors();
}
// Setting values for tabel columns
$data = array(
'product_title' => $this->input->post('product_title'),
'product_description' => $this->input->post('product_description'),
'product_price' => $this->input->post('product_price'),
'product_discount' => $this->input->post('product_discount'),
'product_quantity' => $this->input->post('product_quantity'),
'sub_category_id' => $this->input->post('sub_category_id'),
'category_id' => $this->input->post('category_id'),
'product_status' => $this->input->post('product_status'),
'product_img' => $uploadedFile,
'product_key' => random_string('alnum',8),
'product_cuttingprice' => $this->input->post('product_cuttingprice'),
'size'=>$sizeString);
);
// Transfering data to Model
$prdID = $this->product_model->product_insert($data);
$upImg = $this->uploadAddtnlImages($prdID);
$this->session->set_flashdata('msg', 'Product Added Successfully');
redirect('admin/product/listProduct/', 'refresh');
}
}
View:-
You must do all name checkbox as an array like this.
<div class="custom-file">
<input type="checkbox" name="size[]" value="Medium"> M
<input type="checkbox" name="size[]" value="Large">L
<input type="checkbox" name="size[]" value="XL"> XL
</div>
Controller:-
public function newProduct()
{
$data = array();
$this->load->library('form_validation');
$this->load->library('image_lib');
$this->load->helper('file');
$this->load->helper('string');
// Load the model
$this->load->model('admin/product_model');
$this->load->model('admin/category_model');
$data['category'] = $this->category_model->active_category_listing();
// $data['brand'] = $this->product_model->brand_listing();
$this->form_validation->set_error_delimiters('<div class="alert alert-danger">','</div>');
// Validating Field
$this->form_validation->set_rules('product_title', 'Title', 'required');
if ($this->form_validation->run() == false) {
$this->load->view('admin/add_product', $data);
}
else {
$config['upload_path'] = 'uploads/product_images/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 10024;
$this->load->library('upload', $config);
// upload file to directory
$uploadedFile ='';
if ($this->upload->do_upload('product_img')) {
$uploadData = $this->upload->data();
$uploadedFile = $uploadData['file_name'];
$config['image_library'] = 'gd2';
$config['source_image'] = $uploadData['full_path'];
$config['new_image'] = 'uploads/product_images/thumb';
$config['create_thumb'] = false;
$config['maintain_ratio'] = TRUE;
$config['width'] = 311;
$config['height'] = 415;
$this->image_lib->initialize($config);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$data['success_msg'] = 'File has been uploaded successfully.';
} else {
$data['error_msg'] = $this->upload->display_errors();
}
//insert size checkboxes into database.
$sizeArray = $this->input->post('size'); // Array
$sizeString = implode(",", $sizeArray); // String
// Setting values for tabel columns
$data = array(
'product_title' => $this->input->post('product_title'),
'product_description' => $this->input->post('product_description'),
'product_price' => $this->input->post('product_price'),
'product_discount' => $this->input->post('product_discount'),
'product_quantity' => $this->input->post('product_quantity'),
'sub_category_id' => $this->input->post('sub_category_id'),
'category_id' => $this->input->post('category_id'),
'product_status' => $this->input->post('product_status'),
'product_img' => $uploadedFile,
'product_key' => random_string('alnum',8),
'product_cuttingprice' => $this->input->post('product_cuttingprice'),
'size'=>$sizeString);
);
// Transfering data to Model
$prdID = $this->product_model->product_insert($data);
$upImg = $this->uploadAddtnlImages($prdID);
$this->session->set_flashdata('msg', 'Product Added Successfully');
redirect('admin/product/listProduct/', 'refresh');
}
}
Model Code:-
public function product_insert($data)
{
// grab user input
$this->db->insert('product', $data);
return $this->db->insert_id();
}
Note:- For More info regarding implode().
The implode() function returns a string from the elements of an array.
https://www.php.net/manual/en/function.implode.php
Im new to codeigniter and i have a form with 3 input files that i have to upload and get their path , somehow i cant understand how to do it in codeigniter
here is a part of my form
<?php echo form_open_multipart('drivers/create');?>
............................
..............................
..............................
<div class="form-group">
<label for="">Passport</label>
<input class="form-control" type="file" name="passport" value="">
</div>
<div class="form-group">
<label for="">Driving license</label>
<input class="form-control" type="file" name="driving_license" value="">
</div>
<div class="form-group">
<label for="">Cv</label>
<input class="form-control" type="file" name="cv" value="">
</div>
Than in my controller i have 2 functions create() and upload_image() in create function im trying to call upload_image() to get for each input file path
here is my create()
if ($_FILES['passport']) {
$passport = $this->upload_image($_FILES['passport']);
}
if ($_FILES['driving_license']) {
$driving_license = $this->upload_image($_FILES['driving_license']);
}
if ($_FILES['cv']) {
$cv = $this->upload_image($_FILES['cv']);
}
if ($this->form_validation->run() == TRUE) {
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'phone' => $this->input->post('phone'),
'address' => $this->input->post('address'),
'sort_code' => $this->input->post('sort_code'),
'bank_account' => $this->input->post('bank_account'),
'passport_id' => $passport,
'driving_license' => $driving_license,
'cv' => $cv
);
$create = $this->model_drivers->create($data);
}
here i face the problem for upload_image() how to make for each passed file to return the path?
public function upload_image($name = array()){
..................
...................
$config['upload_path'] = 'assets/drivers';
$config['file_name'] = uniqid();
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$data = array('upload_data' => $this->upload->data());
$type = explode('.', $name['name']);
$type = $type[count($type) - 1];
$path = $config['upload_path'].'/'.$config['file_name'].'.'.$type;
return ($data == true) ? $path : false;
Try pass file array and filename in the function:
if ($_FILES['cv']) {
$cv = $this->upload_image($_FILES['cv'], 'cv');
}
And add upload setup before $data = array('upload_data' => $this->upload->data()); in the modified function:
public function upload_image($file = array(), $name = ''){
..................
...................
$config['upload_path'] = 'assets/drivers';
$config['file_name'] = uniqid();
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$this->load->library('upload');
$this->upload->initialize($config);
$this->upload->do_upload($name);
$data = array('upload_data' => $this->upload->data());
$type = explode('.', $file['name']);
$type = $type[count($type) - 1];
$path = $config['upload_path'].'/'.$config['file_name'].'.'.$type;
return ($data == true) ? $path : false;
I have a form with four fileds like contactemail,contactname,category,comments as of now now i have to add image and doc input in a form,using form i have inserted data to db,how to upload image and doc in codeignatior on form submit
images need to upload in image folder and doc in docs folder
Here is my controller
public function form() {
$this->load->helper('form');
$data = $this->login_model->form();
//loading views
load-view-header
load view page
load-view-footer
}
here my model function
public function form() {
$contactemail = $this->input->post('contactemail');
$contactname = $this->input->post('contactname');
$category = $this->input->post('category');
$comments = $this->input->post('comments');
$data = array(
'contactemail' => $email,
'contactname' => $name,
'category' => $category,
'comments' => $comments
);
$this->db->insert('contact', $data);
return $data;
}
Here is an example. This was put together quickly and is completely untested...at best there may be errors and at worst memory has failed me and I totally missed something but may be a useful example. Let me know if it helps?
View:
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<!-- your other form fields for email, name, category, comments can go here-->
<input type="file" name="image" size="20" />
<input type="file" name="doc" size="20" />
<br /><br />
<input type="submit" value="Submit" />
</form>
Controller:
function form()
{
$this->load->helper(array('form', 'url'));
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$data['error'] = "";
//it would be good to be using the form validation class but here is a quick and dirty check for the submit
if (isset($_POST['submit']))
{
if ( ! $this->upload->do_upload('image'))
{
$data['error'] = $this->upload->display_errors();
}
else
{
$image_upload_data = $this->upload->data();
}
unset($config);
$config['upload_path'] = './docs/';
$config['allowed_types'] = 'doc|docx|txt';
$config['max_size'] = '2048';
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('doc'))
{
$data['error'] .= "<br />".$this->upload->display_errors();
}
else
{
$doc_upload_data = $this->upload->data();
}
if(!empty($data['error']))
{
$this->load->view('form', $data);
}
else
{
$contactemail = $this->input->post('contactemail');
$contactname = $this->input->post('contactname');
$category = $this->input->post('category');
$comments = $this->input->post('comments');
$doc_file = $doc_upload_data['full_path'];
$image_file = $image_upload_data['full_path'];
$form_data = array(
'contactemail' => $email,
'contactname' => $name,
'category' => $category,
'comments' => $comments,
'doc_file' => $doc_file
);
$image_data['image_name'] = $image_file;
$this->login_model->form($form_data,$image_data);
$this->load->view('upload_success', $data);
}
}
else
{
$this->load->view('form', $data);
}
}
Model
public function form($form_data,$image_data) {
$this->db->insert('contact', $image_data);
$image_data['d_fk'] = $this->db->insert_id();
$this->db->insert('images', $image_data);
}
I have an updateform with an file input for adding a picture. What i'm trying to achieve is when I leave that field empty, the file will not be changed. So the photo I've uploaded earlier will stay in there.
My forminput is called: <td><?= form_upload('aanbiedingfoto');?></td>
And my database field is called fotonaam.
I tried something like this, but it did not work:
if($_FILES['aanbiedingfoto']['name'] != '') { $data['fotonaam'] = $_FILES['aanbiedingfoto']['name']; }
What am I doing wrong?
My controller:
function editaanbieding()
{
$data = array(
'Aanbieding' => $this->input->post('aanbiedingnaam'),
'Tekst' => $this->input->post('aanbiedingomschrijving'),
'Prijs' => $this->input->post('aanbiedingprijs'),
'Conditie' => $this->input->post('aanbiedingconditie')
);
if($_FILES['aanbiedingfoto']['name'] != '') { $data['fotonaam'] = $_FILES['aanbiedingfoto']['name']; }
print_r($_FILES);
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '';
$config['max_height'] = '';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['file_name'] = $this->input->post('aanbiedingfoto');
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('aanbiedingfoto'))
{
$error = array('error' => $this->upload->display_errors());
}else{
$image_data = $this->upload->data();
}
$this->aanbieding_model->edit_aanbieding($data, $image_data);
redirect('members/aanbiedingen');
}
My model:
function edit_aanbieding($data, $image_data)
{
$id = $this->uri->segment(3);
$id2 = $this->input->post('fotoid');
$id3 = $this->input->post('aanbiedingid');
/*
echo 'bedrijfaanbiedingid ', $id, '<br/>';
echo 'fotoid ', $id2, '<br/>';
echo 'aanbiedingid ', $id3, '<br/>';
die;
*/
$this->db->where('idaanbiedingen', $id3);
$this->db->update('Aanbiedingen', $data);
$this->db->where('idfotoaanbiedingen', $id2);
$insertfoto = array(
'fotonaam' => $image_data['file_name']
);
$this->db->update('fotoaanbiedingen', $insertfoto);
$this->db->where('idbedrijfaanbiedingen', $id);
}
put your upload code inside the if statement:
<?php
if($_FILES['aanbiedingfoto']['name'] != ''){
if ( ! $this->upload->do_upload('aanbiedingfoto')){
$error = array('error' => $this->upload->display_errors());
}else{
$image_data = $this->upload->data();
}
$this->aanbieding_model->edit_aanbieding($data, $image_data, true);
}else{
$this->aanbieding_model->edit_aanbieding($data, $image_data, false);
}
?>
This is the edit function
<?php
function edit_aanbieding($data, $image_data, $uploaded = false){
$id = $this->uri->segment(3);
$id2 = $this->input->post('fotoid');
$id3 = $this->input->post('aanbiedingid');
/*
echo 'bedrijfaanbiedingid ', $id, '<br/>';
echo 'fotoid ', $id2, '<br/>';
echo 'aanbiedingid ', $id3, '<br/>';
die;
*/
$this->db->where('idaanbiedingen', $id3);
$this->db->update('Aanbiedingen', $data);
$this->db->where('idfotoaanbiedingen', $id2);
if($uploaded){ //update photo code
$insertfoto = array('fotonaam' => $image_data['file_name']);
$this->db->where('idbedrijfaanbiedingen', $id);
$this->db->update('fotoaanbiedingen', $insertfoto);
}
}
?>
If you don't upload any file, $image_data would be NULL.
While there are better solutions, you can fix this as simple as:
Find this section in your model and change it as below:
if (isset($image_data)) {
$insertfoto = array(
'fotonaam' => $image_data['file_name']
);
$this->db->where(...); // Fill the blank with correct arguments
$this->db->update('fotoaanbiedingen', $insertfoto);
}
Side note:
This is completely optional. To increase the performance and get rid of probable errors, I suggest the following:
Avoid of loading libraries or calling methods when not needed.
Set default value for function arguments.
Just as a Demo:
The Controller:
...
if (! empty($_FILES['aanbiedingfoto']['name'])) {
$data['fotonaam'] = $_FILES['aanbiedingfoto']['name'];
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '';
$config['max_height'] = '';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['file_name'] = $this->input->post('aanbiedingfoto');
$this->load->library('upload', $config);
if ($this->upload->do_upload('aanbiedingfoto')) {
$this->aanbieding_model->edit_aanbieding($data, $this->upload->data());
} else {
$error = array('error' => $this->upload->display_errors());
// If you want to update the table while the uploading failed
$this->aanbieding_model->edit_aanbieding($data);
}
}
...
The Model:
function edit_aanbieding($data, $image_data = NULL)
{
...
if (isset($image_data)) {
$insertfoto = array(
'fotonaam' => $image_data['file_name']
);
$this->db->where(...); // Fill the blank with correct arguments
$this->db->update('fotoaanbiedingen', $insertfoto);
}
...
}
I know that this question has been asked several times, but all the other question I found are different from what I want.
I want to upload the filename and filepath to a table called 'factoryimages'.
What's the best way of doing this?
My controller function:
function do_upload()
{
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '';
$config['max_height'] = '';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('members/header');
$this->load->view('members/upload_form', $error);
$this->load->view('members/footer');
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('members/header');
$this->load->view('members/upload_success', $data);
$this->load->view('members/footer');
}
}
I tried this in my model, but it didn't work:
function insert_images()
{
$insert_data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('bedrijfimages', $insert_data);
}
My view:
<?php echo $error;?>
<br/>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" multiple="true" />
<br /><br />
<input type="submit" value="upload" />
</form>
Edit:
I get an server error but i can't see what it is.
My controller function:
function do_upload()
{
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '';
$config['max_height'] = '';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->upload_model->insert_images($this->upload->data());
$this->load->view('members/header');
$this->load->view('members/upload_form', $error);
$this->load->view('members/footer');
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('members/header');
$this->load->view('members/upload_success', $data);
$this->load->view('members/footer');
}
}
My model function:
function insert_images($data = array())
{
$data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('bedrijfimages', $data);
}
What could be the problem?
You must pass this data to your insert as array $this->upload->data()
At your Controller you must set when validation is done
else
{
$this->MODELNAME->insert_images($this->upload->data());
$data = array('upload_data' => $this->upload->data());
$this->load->view('members/header');
$this->load->view('members/upload_success', $data);
$this->load->view('members/footer');
}
And at your model:
function insert_images($image_data = array()){
$data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('bedrijfimages', $data);
}
You can check what information is inside this array at CI documentation page: Codeigniter upload library