【问题标题】:codeigniter, how to save image in specific directorycodeigniter,如何将图像保存在特定目录中
【发布时间】:2017-09-21 00:49:50
【问题描述】:

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

  }

}
?>

如何检查上传图片的状态(成功与否)并将图片上传保存在特定目录中?谢谢你

【问题讨论】:

标签: javascript php html css codeigniter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多