0

i have the code in view/admin_view2.php

<?php echo form_open_multipart('home_admin/createBerita'); ?>
        <div class="form-group" >

            <label class="control-label">upload foto</label>
            <input type="file" name="imagelocs" size="20" />
            <br/>
            <button id="addBtn" style="background-color: #74f442" name="ups">Upload</button>

        </div>

and in model/berita.php

<?php

class berita extends CI_Model {

public function __construct()
{
    $this->load->database();
}


public function process($data)
{
$this->load->database();

$this->db->set($data);
$tes = $this->db->insert($this->db->dbprefix . 'berita');
if($tes)
{
return TRUE;

}
else
{
return FALSE;

}
}   

}

and in controller/home_admin.php

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    class home_admin extends CI_Controller {


    function __construct()
    {
    parent::__construct();
    $this->load->model('berita');
    $this->load->helper(array('form', 'url'));
    }

    public function createBerita()
    {
    $this->load->model('berita');
    $imagelocs = $this->input->post('imagelocs');

    // I NEED SOME CODE TO check status of my upload image (success or not)
    and SAVE image upload in spesific directory.

    $data = array(

      'lokasi_file'=>$lokasi_file,

      );

    $prip = $this -> berita -> process($data);

    if($prip===TRUE){
      $data['message'] = 'Insert success';
        //load your view page
      $this->load->view('success',$data);
    }
    else{
      $data['message'] = 'insert failled';
         // load your view page
      $this->load->view('failed',$data);
    }

  }

}
?>

how can i check status of my upload image (success or not) and SAVE image upload in spesific directory? thankyou

MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
Lukman Azhari
  • 85
  • 1
  • 7
  • https://code.tutsplus.com/tutorials/how-to-upload-files-with-codeigniter-and-ajax--net-21684, https://stackoverflow.com/q/10709647/4229270, https://stackoverflow.com/q/166221/4229270 – Sinto Jul 17 '17 at 13:48

1 Answers1

0

You can add upload function in Controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class home_admin extends CI_Controller {


function __construct()
{
parent::__construct();
$this->load->model('berita');
$this->load->helper(array('form', 'url'));
}

public function createBerita()
{
$this->load->model('berita');
$imagelocs = $this->input->post('imagelocs');

$result = $this->do_upload($imagelocs );
if($result){
echo 'success';
}else{
echo 'false';
}
$data = array(

  'lokasi_file'=>$lokasi_file,

  );

$prip = $this -> berita -> process($data);

if($prip===TRUE){
  $data['message'] = 'Insert success';
    //load your view page
  $this->load->view('success',$data);
}
else{
  $data['message'] = 'insert failled';
     // load your view page
  $this->load->view('failed',$data);
}


}
function do_upload($filename) {
    $config['upload_path'] = 'assets/upload/category/'; // here you can upload image to this directory
    $config['file_name'] = $filename;
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['remove_spaces'] = FALSE;

    $this->load->library('upload', $config);

    if (!$this->upload->do_upload('image_file')) {
        return FALSE;
    } else {
        return TRUE;
    }
}
}
?>
Safan
  • 75
  • 10