How to upload 2 file input in one form Codeigniter 3 - php

I have a problem with how to upload 2 file input in 1 form CI 3 with different file name. My view code: There is have 2 file input, 1. file_sertifikat and 2. file_rekomendasi
<div class="form-group">
<label for="file_sertifikat">File Sertifikat</label>
<input type="file" name="file_sertifikat" class="form-control" id="file_sertifikat">
<small class="text-danger"><?= form_error('file_sertifikat') ?></small>
</div>
<div class="form-group">
<label for="file_rekomendasi">File Rekomendasi</label>
<input type="file" name="file_rekomendasi" class="form-control" id="file_rekomendasi">
<small class="text-danger"><?= form_error('file_rekomendasi') ?></small>
</div>
My Controller action to save filename to database:
$file_sertifikat = $this->upload_file_sertifikat($this->input->post('file_sertifikat'));
$file_rekomendasi = $this->upload_file_rekomendasi($this->input->post('file_rekomendasi'));
$data = array(
'file_sertifikat' => $file_sertifikat,
'file_rekomendasi' => $file_rekomendasi,
);
$this->perizinan_model->insert_data($data,'perizinan');
redirect('admin/perizinan');
}
and my upload file function:
public function upload_file_sertifikat(){
$config['upload_path'] = './assets/uploads';
$config['file_name'] = 'file_sertifikat_'.rand(1, 99999);
$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf';
$config['remove_space'] = TRUE;
$config['max_size'] = 10048;
$this->load->library('upload', $config);
if($this->upload->do_upload('file_sertifikat')){
return $this->upload->data("file_name");
}else{
return "defaultimg.png";
}
}
public function upload_file_rekomendasi(){
$config['upload_path'] = './assets/uploads';
$config['file_name'] = 'file_rekomendasi_'.rand(1, 99999);
$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf';
$config['remove_space'] = TRUE;
$config['max_size'] = 10048;
$this->load->library('upload', $config);
if($this->upload->do_upload('file_rekomendasi')){
return $this->upload->data("file_name");
}else{
return "defaultimg.png";
}
}
I want to upload that 2 different filename one by one, but my code overwrite the file. Thank before.

In html change
<input type="file" name="file_rekomendasi" class="form-control" id="file_rekomendasi">
to
<input type="file" name="file_rekomendasi[]" class="form-control" id="file_rekomendasi" multiple >
In the controller, you will get $file_rekomendasi as an array. Handel that as like array. Run a loop.

Related

Error in adding new item to my database using codeigniter

I have been getting this error while submiting a form using codeigniter. I need help even though I have been following the documentation.
I am using 2.x version of codeigniter
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Products::$input‐
Filename: core/Model.php
Line Number: 52
Fatal error: Call to undefined function post() in /opt/lampp/htdocs/gamingplace/application/models/product_model.php on line 63
This is the code in my model
//add new product
public function addProduct(){
$data = array(
'category_id' => $this->input‐>post('category_id'),
'title' => $this->input‐>post('title'),
'description' => $this->input‐>post('description'),
'price' => $this->input‐>post('price')
);
if($_FILES['image']['error'] == 0){
$data['image'] = $this->upload->file_name;
return $this->db->insert('products',$data);
}
}
}
This is the code in my controller
public function addProduct()
{
if($_FILES['image']['error'] == 0){
//setting image preferences
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'jpg|png|jpeg|PNG|JPEG|JPG';
$config['overwrite'] = false;
$config['quality'] = '100%';
$config['remove_spaces'] = true;
$config['max_size'] = '90';// in KB
$this->load->library('upload',$config);
echo $this->input->post('image');
if( ! $this->upload->do_upload('image'))
{
$this->session->set_flashdata('message_failed', $this->upload->display_errors('', ''));
redirect('dashboard');
}else{
//Image Resizing
$config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
$config['maintain_ratio'] = FALSE;
$config['width'] = 311;
$config['height'] = 162;
$this->load->library('image_lib',$config);
if ( ! $this->image_lib->resize()){
$this->session->set_flashdata('message_failed', $this->image_lib->display_errors('', ''));
}
//calling a model an its method to add a new product to the database
if($this->product_model->addProduct()){
$upload_data = $this->upload->data();
$this->session->set_flashdata('message', 'A new product,'. $upload_data['file_name'].'has been added!');
redirect('dashboard');
}
}
}
}
}
And my view
<!-- Modal -->
<script src="<?php echo base_url(); ?>assets/js/validation.js" charset="utf-8"></script>
<div class="modal fade" id="addProduct" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add Product</h4> <em>if the product category is not in the select option,
go to add category link to add category before adding this product</em>
</div>
<div class="modal-body">
<?php echo validation_errors('<div class="alert alert-danger">','</div>') ?>
<form role= "form" action="<?php echo base_url()?>products/addProduct" method="post" name="form" onsubmit="return validate(this)" enctype="multipart/form-data">
<div class="form-group">
<input type="text" name="title" class="form-control" placeholder="Enter name of product" required>
</div>
<div class="form-group">
<label>Brief Description of Product</label>
<textarea name="description" rows="8" cols="80" class="form-control">
This is a brand made by Gucci Company. Made of cotton, bla bla bla
</textarea>
</div>
<label>Select Category</label>
<div class="form-group">
<select class="form-control" name="category_id">
<?php foreach (get_categories_h() as $popular): ?>
<option value="<?php echo$popular->id?>"><?php echo $popular->name ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label>Enter or choose price</label>
<input type="number" id="price" class="form-control" name="price_old" required>
</div>
<input type="hidden" name="price" class="form-control" id="mainPrice"required>
<div class="form-group">
<input type="file" name="image" id="image" class="form-control" required accept="image/*" onChange="validateImage(this.value)">
</div>
<button type="submit" name="submit" class=" form-control btn btn-success">Add</button>
</form>
</form>
</div>
</div>
</div>
</div>
I think you may have a few issues going on with your code, but let's first focus on your question/error you mentioned. In your controller function, you are not passing the post to the model. You need to do something like this in the controller:
$addProduct = $this->product_model->addProduct($_POST);
if($addProduct){
$upload_data = $this->upload->data();
$this->session->set_flashdata('message', 'A new product,'. $upload_data['file_name'].'has been added!');
redirect('dashboard');
}
In the model function, you need to first let the function know there is an incoming parameter, then adjust how you are assigning the fields to the data array.
public function addProduct($post){
$data = array(
'category_id' => $post["category_id"],
'title' => $post["title"],
'description' => $post["description"],
'price' => $post["price"]
);
return $this->db->insert('products',$data);
}
After that, I noticed that you have some other FILES logic in your model. That will likely error out, too. I believe that is going to have to be done in the controller, not the model. Once you do the upload in the controller, you can pass the file name into the model as well.
$upload_data = $this->upload->data();
$addProduct = $this->product_model->addProduct($_POST, $upload_data['file_name']);
if($addProduct){
$this->session->set_flashdata('message', 'A new product,'. $upload_data['file_name'].'has been added!');
redirect('dashboard');
}
Then, adjust the model.
public function addProduct($post, $fileName){
$data = array(
'category_id' => $post["category_id"],
'title' => $post["title"],
'description' => $post["description"],
'price' => $post["price"],
'image' => $fileName
);
return $this->db->insert('products',$data);
}
Hope this helps.

Codeigniter file upload error: You did not select a file to upload

This is really odd, I tried to check the top suggestions in google that is similar to my issue here but I believe those are not the answers to this. I'm using the default 'userfile'. Can anyone tell me what I'm missing here?
The structure of my form:
<div class="panel-body">
<?php echo form_open_multipart('register','role="studentForm" class="form-horizontal'); ?>
<div class="form-group">
<label for="userfile" class="col-lg-3 col-md-3 col-sm-3 control-label">Profile Picture</label>
<div class="col-lg-9 col-md-9 col-sm-9">
<input type="file" name="userfile" size="20" id="userfile">
</div>
</div>
<button type="submit" class="btn btn-default btn-login-style btn-sm pull-right">Submit</button>
<?php echo form_close(); ?>
</div>
The structure of my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 2048;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->upload->initialize($config);
if(!$this->upload->do_upload()){
$error = array('error'=>$this->upload->display_errors());
echo $error['error'];
}else{
$uploaded_pic = $this->upload->data();
}
}
}
?>
NOTE: 'Upload' library is loaded automatically in autoload.php.
try removing $this->upload->do_upload(); before the if else statement?
don't need to specify twice.
You have called $this->upload->do_upload(); twice. Pls remove the first one.
And use $this->upload->do_upload('userfile'); in place of $this->upload->do_upload();
ok i got it. I am missing a quotation " in my class:
the wrong code:
<?php echo form_open_multipart('register','role="studentForm" class="form-horizontal'); ?>
the correction here:
<?php echo form_open_multipart('register','role="studentForm" class="form-horizontal"'); ?>
The Second Line in Your View,Please Correct it
<?php echo form_open_multipart('register','role="studentForm" class="form-horizontal" '); ?>

codeigniter $this->upload->do_upload() = false

I'm trying to upload a file. I select a file and then I submit it but result of $this->upload->do_upload() is always false.
Here is my form;
<?php echo form_open_multipart(base_url('files/fileUpload')); ?>
<input type="file" name="userfile" class="btn btn-default" size="20"/>
<input type="submit" value="upload" />
</form>
and here is my controller;
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function fileUpload(){
$this->load->library('upload');
$data = array('upload_data' => $this->upload->data());
var_dump($this->upload->do_upload());die;
}
Do I miss something ? What do I need to do ?
Thank you..
There is problem with code in controller fileUpload function
Try with below code
public function fileUpload(){
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload',$config);
$data = array('upload_data' => $this->upload->data());
var_dump($this->upload->do_upload());die;
}
Here you need to define file upload path and file allow types.
Hope this will help you.

Uploading file with php

I have the following set up in a Code Igniter application, however the file is not being passed through to the file uploader class. This is the second form on the page, just for reference and I have checked that they are both closed correctly.
The output I am getting is You did not select a file to upload. when I have been selecting an valid image.
View
<form action="../albums/upload" class="form-horizontal" method="post">
<div class="form-group">
<input type="file" name="userfile" required="" class="form-control">
</div>
<div class="form-group">
<div class="col-md-4 col-md-offset-4">
<input type="submit" id="upload" name="upload" class="btn btn-success" value="Upload">
</div>
</div>
</form>
Controller
public function upload() {
if($this->input->post('userfile')){
$this->model_photo->do_upload();
}
}
Model
function do_upload() {
$path = base_url().'res/image/';
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png|tif|tiff',
'upload_path' => $path
);
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
echo $error['error']; //echo debugging purposes
} else {
$data = array('upload_data' => $this->upload->data());
echo $data['upload_data']; //echo debugging purposes
}
}
in your <form> add enctype="multipart/form-data"
<form action="../albums/upload" class="form-horizontal" method="post" enctype="multipart/form-data">

image uploading is not working in CodeIgniter

when I click button then it will return error "You did not select a file to upload." and My image is not uploaded!
now please help me what can I do?
my view is
<form action="insertform" enctype="multipart" name="form" method="post" id="contact-form" class="form-horizontal">
<div class="control-group">
<label class="control-label" for="name">Image Upload:</label>
<div class="controls">
<input type="file" class="input-large" name="image" id="image">
</div>
<form>
my controller is:
public function upload()
{
$this->load->helper(array('form', 'url'));
$config['upload_path'] = FCPATH."upload";
$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('xyz', $error);
} else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('xyz', $data);
}
}
I think the file input name should be "userfile", not "image".
CI file upload file control default name is userfile.
If you want to change the it's name to customized one, then you have to pass the new name to upload function like :
if ( ! $this->upload->do_upload("image")) // image is your new name for file control
Firstly,
either change the do_upload function call to:
if ( ! $this->upload->do_upload('image'))
OR
change the input name to this:
<input type="file" class="input-large" name="userfile" id="image">
Secondly,
change your form enctype to:
enctype="multipart/form-data"

Categories