codeigniter,如何在特定目录中保存图像

时间:2017-07-17 13:45:25

标签: javascript php html css codeigniter

我有 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>

并在 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;

}
}   

}

并在 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);
    }

  }

}
?>

我如何检查上传图片的状态(成功与否)以及在特定目录中保存图片?三江源

1 个答案:

答案 0 :(得分:0)

您可以在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;
    }
}
}
?>