uploading photo codeigniter not inserting into the right column - php

i manage to insert the uploaded photo into databases as well as into the specified path, the problem is the photo that i uploaded not inserting to the logging user column instead its create another column, am i missing something? below are my codes and also screenshot in case you guys hardly understand what i meant
MODEL
function create($data){
$query = $this->db->insert('dosen', $data);
return $query;
}
CONTROLLER
public function Upload(){
$upload = $this->input->post('fotoDsn');
//Foto Set
$photoName = gmdate("d-m-y-H-i-s", time()+3600*7).".jpg";
$config['upload_path'] = './assets/';
$config['allowed_types'] = 'gif||jpg||png';
$config['max_size'] = '2048000';
$config['file_name'] = $photoName;
$this->load->library('upload',$config);
if($this->upload->do_upload('userfile')){
$upload = 1;
}
else{
$upload = 2;
}
if($upload==1){
$data = array(
'foto_dosen'=>$photoName);
$insert = $this->MDosen->create($data);
if($insert){
echo 1;
}else{
echo 2;
}
}//else kalo gagal
}
VIEW
<i class="material-icons tiny">assignment_ind</i> Upload
<div id="modalUpload" class="modal" style="width: 40%; height: auto">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<i class="medium material-icons prefix">assignment_ind</i>
<h4 class="modal-title"> Upload Foto</h4>
</div>
<div class="modal-body">
<form action="upload" id="tambahFormUpload" method="post" enctype="multipart/form-data">
<div class="file-field input-field">
<div class="btn">
<span>File</span>
<input type="file" name="userfile">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" >Simpan</button>
</div>
</form>
</div>
</div>
</div>
</div>
JS
$('.upload').click(function(){
id1 = $(this).attr('id');
});
$('#tambahFormUpload').submit(function(e){
alert(id1);
e.preventDefault();
var formData = new FormData($(this)[0]);
$.ajax({
url:'Profil/Upload/'+id1,
data:formData,
type:'POST',
contentType: false,
processData: false,
success:function(data){
$("#modalUpload").hide();
window.location.reload(true);
}
});
});

based on #Sean comment it is true that instead using insert, we should use update, so the correct controller will look like this, using update model rather than create and put parameter on my function, on second though the reason why it's not insert into the correct column because the id of the user can't be found (where clause), so with update we can set the where clause and point it into the user id, Hope this help
CONTROLLER
public function Upload($id){
$upload = $this->input->post('fotoDsn');
//Foto Set
$photoName = gmdate("d-m-y-H-i-s", time()+3600*7).".jpg";
$config['upload_path'] = './assets/';
$config['allowed_types'] = 'gif||jpg||png';
$config['max_size'] = '2048000';
$config['file_name'] = $photoName;
$this->load->library('upload',$config);
if($this->upload->do_upload('userfile')){
$upload = 1;
}
else{
$upload = 2;
}
if($upload==1){
$data = array(
'foto_dosen'=>$photoName);
$update = $this->MDosen->update(array('id'=>$id), $data);
if($update){
echo 1;
}else{
echo 2;
}
}//else kalo gagal
}
MODEL
function update($cond, $data){
$this->db->where($cond);
$query = $this->db->update('dosen', $data);
return $query;
}

Related

File not selected in form multiple upload in Codeigniter

so i try to make some upload form which will upload some image. but when the image has chosen then the button submit has clicked the result say "You did not select a file to upload."
im using php, with codeigniter framework
The view
<form class="text-left" enctype="multipart/form-data" method="post" action="<?= base_url('aduan/add'); ?>">
<div class="form-group">
<label>Upload Foto Bukti Aduan</label>
<div class="form-group">
<label>Bukti Aduan 1</label>
<input class="form-control-file" type="file" id="bukti1" name="bukti_aduan1" />
<?= form_error('bukti_aduan1', '<p class="text-danger font-weight-bold pl-2">', '</p>'); ?>
</div>
<div class="form-group">
<label>Bukti Aduan Pelapor di Lokasi</label>
<input class="form-control-file" type="file" id="bukti2" name="bukti_aduan2" />
<?= form_error('bukti_aduan2', '<p class="text-danger font-weight-bold pl-2">', '</p>'); ?>
</div>
</div>
which the view will run the controller "aduan" for input function
Controller file:
$aduan = $this->aduan_model;
$validation = $this->form_validation;
$validation->set_rules($aduan->rules());
if ($validation->run()) {
$aduan->tambah();
$this->session->set_flashdata('success', 'Berhasil disimpan');
$data['title'] = 'Buat Aduan Kasus Lingkungan';
$this->load->view('home/home_header', $data);
$this->load->view('home/aduansukses');
$this->load->view('home/home_footer');
} else {
$data['aduan'] = $this->aduan_model->getAll();
$data['title'] = 'Buat Aduan Kasus Lingkungan';
$this->load->view('home/home_header', $data);
$this->load->view('home/buataduan');
$this->load->view('home/home_footer');
}
then the controller will load a model which is "aduan_model"
Model file :
public function tambah()
{
$post = $this->input->post();
$this->nik_pelapor = $post["nik_pelapor"];
$this->nama_pelapor = $post["nama_pelapor"];
$this->email_pelapor = $post["email_pelapor"];
$this->telp_pelapor = $post["telp_pelapor"];
$this->alamat_pelapor = $post["alamat_pelapor"];
$this->aduan = $post["aduan"];
$this->bukti_aduan1 = $this->_uploadImage();
$this->bukti_aduan2 = $this->_uploadImage2();
$this->status_aduan = 'Dalam proses';
$this->tgl_aduan = time();
$this->db->insert($this->_table, $this);
}
private function _uploadImage()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
// $config['max_width'] = 1024;
// $config['max_height'] = 768;
$this->load->library('upload', $config);
if ($this->upload->do_upload('bukti_aduan1')) {
return $this->upload->data();
}
return "default.jpg";
}
private function _uploadImage2()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
// $config['max_width'] = 1024;
// $config['max_height'] = 768;
$this->load->library('upload', $config);
if ($this->upload->do_upload('bukti_aduan2')) {
return $this->upload->data();
}
return "default.jpg";
}
i expect the output of image can be stored in file upload and inserted in database but it doesnt select any file
please help :)
try my code
replace if condition in controller to upload
if(
(!$this->upload->do_upload('bukti_aduan2')) &&
(! $this->upload->do_upload('bukti_aduan1'))
)
{
echo "failed";
}
else
{
echo "success";
}

How to upload image file in Codeigniter

I can't upload an image file, I don't know the input file and can't detect it. That's why I can't upload a file.
This is the controller, I named it desain.php.
defined('BASEPATH') OR exit('No direct script access allowed');
class Desain extends CI_Controller {
public function index()
{
$query = "select * from result,kategori where result.id_cat=kategori.id_cat";
$tampil = $this->db->query($query)->result();
$this->load->view('Admin/data.php', ['tampil' => $tampil]);
}
public function do_upload()
{
$config['upload_path'] = './asset/upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048000';
$config['max_width'] = '1024';
$config['max_height'] = '1024';
$config['file_name'] = $this->input->post('photo');
$this->load->library('upload',$config);
if(!$this->upload->do_upload('photo'))
{
$error = array ('error' =>$this->upload->display_errors());
redirect('admin/desain');
}
else { $data = $this->upload->data($config);
$data = array('photo' =>$this->upload->data($config),
'nama_desain' =>$this->input->post('nama'),
'id_cat' =>$this->input->post('categori'),
'ket' =>$this->input->post('ket'),
);
$this->db->insert('result',$data);
redirect('admin/desain');
}
}
}
This is the view:
<form role="form" action="<?php echo base_url();?>admin/desain/do_upload" method="post" enctype="multipart/form-data">
<div class="box-body">
<div class="form-group">
<label for="exampleInputEmail1">Nama Desain</label>
<input type="text" class="form-control" name ="nama" id="nama" placeholder="Nama Desain">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Kategori</label>
<select class="form-control" id="categori" name="categori">
<option value="">Pilih Kategori</option>
<option value="1">Vektor</option>
<option value="2">Editing</option>
<option value="3">Desain</option>
</select>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Keterangan Gambar</label>
<textarea class="form-control" rows="3" name="ket" id="ket" placeholder="Enter ..."></textarea>
</div>
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input type="file" required="required" id="photo" name="photo" class="form-control">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
I tried to post this form, but this file input is NULL.
Try this hope it will help you
$config['upload_path'] = './asset/upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048000;
$config['max_width'] = 1024;
$config['max_height'] = 1024;
$config['file_name'] = $this->input->post('photo');
$this->load->library('upload', $config);
if (!$this->upload->do_upload($config['file_name']))
{
$error = $this->upload->display_errors();
//coding...
}
else
{
$filename = $this->upload->data();
$data[$config['file_name']] = $filename['file_name'];
//coding...
}
You should catch the file by
$config['file_name'] = $_FILES['name']['photo'];
Than you will be able to get the image file to process.
Before it please check if the field is able to get or not by
if(!empty($_FILES['name']['photo']))
This was perfectly working on my computer. Hope it will be helpful for you.
You don't need to do this: $config['file_name'] = $this->input->post('photo');
By default the upload class will use the name of the image as the name of the final image. If you wanted to do it this way than $_FILES['photo']['name']; would work (but again, there is no need).
Further you aren't getting the value of the uploaded photo correctly. I'm not sure why you are putting the config array in this function: $this->upload->data($config); but that is not how it supposed to be used.
If you wanted to get the filename for example you could do:
$filename = $this->upload->data('file_name');
OR
$up_data = $this->upload->data();
$filename = $up_data['file_name']
See this for reference: https://www.codeigniter.com/userguide3/libraries/file_uploading.html#CI_Upload::data
Also you are not doing anything with this line: $error = array ('error' =>$this->upload->display_errors());. I suggest setting the error as a flash message before redirecting.
if(!$this->upload->do_upload('photo'))
{
$error = array ('error' =>$this->upload->display_errors());
redirect('admin/desain');
} else {
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
$data = array(
'photo' => $uploadData['file_path'].'upload_images/'. $picture,
'nama_desain' =>$this->input->post('nama'),
'id_cat' =>$this->input->post('categori'),
'ket' =>$this->input->post('ket'),
);
$this->db->insert('result',$data);
redirect('admin/desain');
}

image upload in codeigniter giving error

I am upload an image in one my form but its always giving me an error like
"A PHP Error was encountered Severity: Notice Message:
Undefined index: vimage Filename: controllers/vouchers.php Line
Number: 42"
My View File code :
<?php echo form_open_multipart(site_url("promotions/add_item/"), array("class" => "form-horizontal","id"=>"addItem")) ?>
<div class="form-group">
<label class="col-sm-3 control-label">Show on</label>
<div class="col-sm-3">
Artist Directory : <input type="checkbox" name="artist_dir" value='1'>
</div>
<div class="col-sm-3">
Highlight : <input type="checkbox" name="highlight" value='1'>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Title</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="title" id="title" placeholder="Title">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Image</label>
<div class="col-sm-9">
<input type="file" name="pro_image">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Description</label>
<div class="col-sm-9">
<textarea class="form-control" rows="3" name="description" placeholder="Description"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-success" name="submit" id="submit">Submit</button>
</div>
</div>
<?php echo form_close() ?>
My Controller Code :
<pre>
public function add_item(){
$this->load->library('upload');
$this->form_validation->set_rules('title','Title','required');
$this->form_validation->set_rules('description','Description','required');
print_r($_FILES['pro_image']);
if ($_FILES['pro_image']['size'] > 0) {
$this->upload->initialize(array(
"upload_path" => './uploads/',
"overwrite" => FALSE,
"encrypt_name" => TRUE,
"remove_spaces" => TRUE,
"allowed_types" => "gif|jpg|png|jpeg",
));
if (!$this->upload->do_upload('vimage')) {
$this->upload->display_errors();
}
$data = $this->upload->data();
echo 'img : '.$img = $data['file_name'];
}
exit;
if($this->form_validation->run()==FALSE){
echo '<div class="alert alert-dismissable alert-danger"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button><small>'. validation_errors().'</small></div>';
}
else {
$title = $this->input->post('title');
$description = $this->input->post('description');
$artist_dir = $this->input->post('artist_dir');
$highlight = $this->input->post('highlight');
$this->promotions_model->add_item($title, $description,$artist_dir,$highlight);
}
}
</pre>
please let me know what i am doing wrong here
Try this
if ($_FILES['vimage']['size'] > 0) {
$this->load->library('upload');
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$fileName = $_FILES['vimage']['name'];
private function set_upload_options()
{
// upload an image options
$config = array();
$config['upload_path'] = './upload/'; //give the path to upload the image in folder
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
}
For more read this Link
Try this
if (!empty($_FILES['vimage']))
{
$config['upload_path'] = './uploads/';
//$config['max_size'] = '102400';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['overwrite'] = FALSE;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('vimage'))
{
$this->session->set_flashdata('error', $this->upload->display_errors());
//redirect('controller/method');
}
else
{
$this->session->set_flashdata('success', 'Image Has been Uploaded');
//redirect('controller/method');
$img = $data['file_name'];
}
}
Please check first,
print_r(_FILES);
If you get files array then you can use below code for uploading image.
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';
// You can give Image formats if you want to upload any video file.
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' = $this->upload->display_errors());
// uploading failed. $error will holds the errors.
}
else
{
$data = array('upload_data' => $this->upload->data());
// uploading successfull, now do your further actions
}
}
I have added your code in my system it's working for fine for me.
public function add(){
$this->load->library('upload');
$this->load->helper('form');
$this->load->helper('url');
print_r($_FILES);die;
$this->form_validation->set_rules('title','Title','required');
$this->form_validation->set_rules('description','Description','required');
if ($_FILES['pro_image']['size'] > 0) {
$this->upload->initialize(array(
"upload_path" => './uploads/',
"overwrite" => FALSE,
"encrypt_name" => TRUE,
"remove_spaces" => TRUE,
"allowed_types" => "gif|jpg|png|jpeg",
));
if (!$this->upload->do_upload('vimage')) {
$this->upload->display_errors();
}
$data = $this->upload->data();
echo 'img : '.$img = $data['file_name'];
}
exit;
if($this->form_validation->run()==FALSE){
echo '<div class="alert alert-dismissable alert-danger"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button><small>'. validation_errors().'</small></div>';
}
else {
$title = $this->input->post('title');
$description = $this->input->post('description');
$artist_dir = $this->input->post('artist_dir');
$highlight = $this->input->post('highlight');
$this->promotions_model->add_item($title, $description,$artist_dir,$highlight);
}
}

File was not uploading in codeigniter

I am uploading images in codeigniter. my file is not getting uploaded. i dont know where i am doing wrong. please find in below code
Html:
<form method="post" enctype="multipart/form-data" action="<?php echo base_url(); ?>assign_user/addStartPopup">
<div class="col-lg-6">
<div class="form-group">
<label for="image">
<input type="radio" id="image" name="start_img" onclick="rightfilefunction(1);" checked> <label for="image"> <span style="top: -2px;position: relative;">Image upload</span></label>
<label for="video" class="green">
<input type="radio" id="video" name="start_img" onclick="rightfilefunction(2);">
<span style="top: -2px;position: relative;">Video</span>
</label>
</div>
<div class="form-group" id="imageId">
<input type="file" name="imageupload" class="form-control">
</div>
<div class="form-group" id="videoId" style="display:none">
<input type="text" name="video_upload" class="form-control">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary"> Save </button>
</div>
</div>
</form>
Model:
public function startPopup(){
$createdOn = date('Y-m-d H:i:s');
if($_FILES['imageupload']['size'] != 0)
{
$files = $_FILES;
$config = array();
$config['upload_path'] = "startpage/";
$config['allowed_types'] = "png|jpg|gif";
$config['max_size'] = "5000";
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('imageupload')){
$imgdata = $this->upload->data();
$image_config=array();
$image_config["image_library"] = "gd2";
$image_config['overwrite'] = TRUE;
$image_config["source_image"] = $imgdata["full_path"];
$image_config['create_thumb'] = FALSE;
$image_config['maintain_ratio'] = TRUE;
$image_config['new_image'] = $imgdata["file_path"].$imgdata["file_name"];
$image_config['quality'] = "95%";
$image_config['width'] = 170;
$image_config['height'] = 170;
$this->load->library('image_lib',$image_config);
$this->image_lib->initialize($image_config);
$this->image_lib->resize();
$logo = 'startpage/'.$imgdata["file_name"];
$query = $this->db->query("INSERT INTO `startpop` (content_view, created_on) VALUES ('$logo', '$createdOn')");
if($query){
return 1;
}
}else
{
echo "not uploaded";
}
}else{
$prv = $this->input->post('video_upload');
$query = $this->db->query("INSERT INTO `startpop` (content_view, created_on) VALUES ('$prv', '$createdOn')");
if($query){
return 1;
}
}
}
Controller:
public function addStartPopup(){
$result = $this->model->startPopup();
if($result > 0){
$data['mgs'] = "Added Successfully ..!";
$this->load->view('admin/startPage',$data);
}
}
From this above code, i am getting the error "not uploaded". can any one tell me where i am doing wrong

Codeigniter file upload not working

I write code for upload a image file. But it always show You did not select a file to upload. This is my code.
view
<?php echo form_open_multipart('template/advertisement', array('id' => 'template-add-form', 'class' => 'form-horizontal')); ?>
<?php echo form_fieldset('Add Advertisement', array('class' => '')); ?>
<div class="control-group">
<label class="control-label" for="add_l">Left Advertisement </label>
<div class="controls">
<input type="file" name="add_l"/>
</div>
</div>
<div class="control-group">
<div class="controls">
<input type="submit" name="add" value="Update Advertisement" class="btn btn-primary" />
</div>
</div>
<?php echo form_fieldset_close(); ?>
<?php echo form_close(); ?>
controller
public function advertisement()
{
$headerData = array();
$headerData['title'] = ucfirst('Advertisement');
$data = array();
if ($this->input->post() !== FALSE) {
$config = array();
$config['upload_path'] = './images/add';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['max_filename'] = '100';
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$data['message'] = $this->upload->data();
} else {
$data['error'] = $this->upload->display_errors('<div class="alert alert-error">', '</div>');
}
}
$this->load->view('template/header.php', $headerData);
$this->load->view('template/advertisement.php', $data);
$this->load->view('template/footer.php');
}
Please help me.
At your PHP change
$this->upload->do_upload()
to
$this->upload->do_upload("add_1")
where "add_1" is your input name attribute.
Codeigniter UPLOAD library docs
P.S. At your HTML the Label attribute for="" is targeting input ID so you have to add id to your input.
i recenly got that problem and solve it. you can check is module fileinfo in php already on? if you can access to CPanel, you can go to SOFTWARE TAB then search Select PHP Version, then you can see list of module and check that module fileinfo. and try to upload again.

Categories