使用Codeigniter 3.0.1在db中上传图像

时间:2016-04-27 11:29:57

标签: php mysql codeigniter

我读过许多表格/答案对CI 3.0+没什么帮助。试图将图像路径上传到mysql数据库。看起来我的Update查询无效(图像列在运行查询之前没有任何内容)。这是我的控制器处理上传内容:

我从CI文档 upload.php

中获取了它
<?php

class Upload extends CI_Controller {

    public function __construct()
    {
            parent::__construct();
            // $this->load->helper(array('form', 'url'));
            $this->load->library('upload');
    }

    public function index()
    {
            $this->load->view('layouts/header');
            $this->load->view('home_page', array('error' => ' ' ));
            $this->load->view('layouts/footer');
    }

    public function do_upload()
    {

            $config['upload_path']          = './uploads/'; #$this->config->item('base_url').
            $config['allowed_types']        = 'gif|jpg|png|jpeg';
            // $config['max_size']             = 100;
            // $config['max_width']            = 1024;
            // $config['max_height']           = 768;

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

            $this->upload->initialize($config);

            if ( ! $this->upload->do_upload('userfile'))
            {
                    $error = array('error' => $this->upload->display_errors('<p>','</p>'));

                    $this->load->view('layouts/header');
                    $this->load->view('home_page', $error);
                    $this->load->view('layouts/footer');
            }

            else
            {

                    $this->load->model('model_edit');
                    $id = $this->session->userdata('id');
                    $data = array('upload_data' => $this->upload->data());
                    $image['img'] = $this->upload->data('file_name');


                    $this->model_edit->update_dp($id, $data);

                     #base_url().'upload/'.

                    $this->load->view('layouts/header');
                    // echo $id;
                    $this->load->view('home_page', $image);
                    $this->load->view('layouts/footer');
            }
    }
    }
    ?>

这是我的模型类 model_edit.php

<?php

class Model_edit extends CI_Model
{

public function __construct()
{
    parent::__construct();
}
public function update_dp($id, $data) {

    $image_path = $this->upload->data('file_path');
    $sql = "UPDATE users SET images = '{$image_path}' WHERE id = '{$id}' LIMIT 1";
    $result = $this->db->query($sql);
    $row = $this->db->affected_rows();

    if ($row) {
        return $image_path;
    }
    else
    return FALSE;       
 } 
}
?>

这是我使用 home_page.php

的观点
<?php
$session_data = $this->session->userdata('logged_in');
$check = $this->session->userdata('remember_me');

?>
<div class="container">
<div class="row">
    <?php include('navbar.php'); ?>
</div>
<div class="row">
    <div class="col-xs-10">
        <?php

            if ($check) 
            {?>
            <div class="row">
              <div class="col-xs-4 col-xs-3">
              <?php if (isset($upload_data)) 
              {
                echo "Successfully Uploaded DP";
                echo $img;
                ?>
                <img src="<?php echo $img; ?>"/>
                <?php
              }?>               
                <?php if(isset($error))echo $error;  echo form_open_multipart('upload/do_upload');?>

                <input type="file" name="userfile"/>
                <br />
                <input type="submit" name="submit" value="upload" />

                </form>
              <hr>
              </div>
              <div class="col-xs-6 col-xs-5">

              </div>
            </div>
            <div class="row">  
              <div class="col-xs-4 col-xs-3">
              <label class="text-center"> User Information </label><a href="edit"><span class="glyphicon glyphicon-pencil pull-right"></span></a>
              <br><br>
                <label>Email: </label>
                <br>
                <?php echo $session_data['email'];?>
                <br><br>
                <label>Display Name: </label>
                <br>
                <?php echo $session_data['name'];?>
                <br><br>
                <label>Username: </label>
                <br>
                <?php echo $session_data['username'];?>
                <br><br>
                <a href="logout">Logout</a>
              </div>
             </div>

            <?php

            }
            else
            {
                // session_set_cookie_params(0);
                 include('browser_triger.php');
                 $session_data['username'];
                 $session_data['name'];
                 $session_data['email'];
            }
        ?>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

您需要在控制器中获取图像路径,并仅将图像名称传递给模型文件

在控制器中

$image_path = $this->upload->data('file_name');
$this->model_edit->update_dp($id, $file_name);// pass image name

在模型中

function update_dp($id, $file_name) {
    $this->db->set("images", $file_name);
    $this->db->where("id", $id);
    $this->db->update("users");
    $row = $this->db->affected_rows();

    if ($row) {
        return $image_path;
    } else{
        return FALSE;
    }
}