使用Codeigniter 3.0.1在db中上传图像

哈比卜·雷曼(Habib Rehman)

我读过许多表格/答案对CI 3.0+没多大帮助。尝试将映像路径上传到mysql数据库。似乎我的Update查询无法正常工作(运行查询之前image列没有任何内容)。这是我的控制器,它处理上传内容:

我已经从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>

衣服

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

在控制器中

$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;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Codeigniter 3.0.1在db中上传图像

来自分类Dev

使用Codeigniter 3插入DB

来自分类Dev

如何使用PHP在Selenium中上传文件?预计只有1个文件。共有:0

来自分类Dev

使用Codeigniter 3上传多个文件

来自分类Dev

使用Ajax在MVC4中上传图像时,Request.Files.Count始终为0

来自分类Dev

从 POST 的图像数组中上传 1 个特定索引,而不通过 $_FILES 循环 - 在 CodeIgniter 中

来自分类Dev

使用干预在 Laravel 中上传图像

来自分类Dev

无法使用 AJAX 在 codeigniter 中上传文件

来自分类Dev

在Codeigniter中上传之前如何验证图像宽高比

来自分类Dev

如何在Codeigniter中上传2张单独的图像

来自分类Dev

在数据库中的Codeigniter中上传图像

来自分类Dev

尝试在Codeigniter PHP中上传多个调整大小的图像

来自分类Dev

在codeigniter中上传具有特定宽度和高度的图像

来自分类Dev

使用 codeigniter 显示上传的图像

来自分类Dev

使用codeigniter 3上传不同类型的文件?

来自分类Dev

无法使用laravel 5.7在s3存储桶中上传文件-参数1传递给League \ Flysystem \ AwsS3v3 \ AwsS3Adapter:

来自分类Dev

在Codeigniter中上传文件

来自分类Dev

在Codeigniter中上传文件

来自分类Dev

在Codeigniter中上传图片

来自分类Dev

在codeigniter中上传文件

来自分类Dev

使用Scala收集方法可帮助将[0,0,0,1,1,1,1,0,0,1,1]的列表转换为[3,4,2,2]

来自分类Dev

使用Ajax使用图像上传Codeigniter表单

来自分类Dev

如何使用 Codeigniter 中的不同按钮在单个表单中上传多个图像和文本文件?

来自分类Dev

如何使用httpClient.postAsync在UWP中上传图像或bytes []

来自分类Dev

使用JavaScript在Spring MVC中上传图像进行预览

来自分类Dev

使用Multer在Node.js中上传多个图像

来自分类Dev

我可以仅使用URL在GDrive中上传图像吗?

来自分类Dev

如何使用强大的模块在Node JS中上传多个图像

来自分类Dev

使用PHP在Web服务中上传多个图像

Related 相关文章

  1. 1

    使用Codeigniter 3.0.1在db中上传图像

  2. 2

    使用Codeigniter 3插入DB

  3. 3

    如何使用PHP在Selenium中上传文件?预计只有1个文件。共有:0

  4. 4

    使用Codeigniter 3上传多个文件

  5. 5

    使用Ajax在MVC4中上传图像时,Request.Files.Count始终为0

  6. 6

    从 POST 的图像数组中上传 1 个特定索引,而不通过 $_FILES 循环 - 在 CodeIgniter 中

  7. 7

    使用干预在 Laravel 中上传图像

  8. 8

    无法使用 AJAX 在 codeigniter 中上传文件

  9. 9

    在Codeigniter中上传之前如何验证图像宽高比

  10. 10

    如何在Codeigniter中上传2张单独的图像

  11. 11

    在数据库中的Codeigniter中上传图像

  12. 12

    尝试在Codeigniter PHP中上传多个调整大小的图像

  13. 13

    在codeigniter中上传具有特定宽度和高度的图像

  14. 14

    使用 codeigniter 显示上传的图像

  15. 15

    使用codeigniter 3上传不同类型的文件?

  16. 16

    无法使用laravel 5.7在s3存储桶中上传文件-参数1传递给League \ Flysystem \ AwsS3v3 \ AwsS3Adapter:

  17. 17

    在Codeigniter中上传文件

  18. 18

    在Codeigniter中上传文件

  19. 19

    在Codeigniter中上传图片

  20. 20

    在codeigniter中上传文件

  21. 21

    使用Scala收集方法可帮助将[0,0,0,1,1,1,1,0,0,1,1]的列表转换为[3,4,2,2]

  22. 22

    使用Ajax使用图像上传Codeigniter表单

  23. 23

    如何使用 Codeigniter 中的不同按钮在单个表单中上传多个图像和文本文件?

  24. 24

    如何使用httpClient.postAsync在UWP中上传图像或bytes []

  25. 25

    使用JavaScript在Spring MVC中上传图像进行预览

  26. 26

    使用Multer在Node.js中上传多个图像

  27. 27

    我可以仅使用URL在GDrive中上传图像吗?

  28. 28

    如何使用强大的模块在Node JS中上传多个图像

  29. 29

    使用PHP在Web服务中上传多个图像

热门标签

归档