Codeigniter图像路径不存储在数据库中

时间:2017-03-08 13:22:38

标签: php codeigniter file-upload

嗨朋友们,我的第一个问题就是在这里,我希望能让你理解我想要的东西,因为我是codeigniter的新手,也不是很好的英语:p

所以我的问题是我正在尝试使用Codeigniter上传图像我有两个文件夹 1.(完整图像) 2.(对于thumnils图像) 但当我上传它给我错误消息

  

列'img_path'不能为空

INSERT INTO `gallery` (`img_path`, `img_name`) VALUES (NULL, 'asdasd')

这是我的控制器模型和视图

控制器:

`

<?php
class Gallery extends CI_Controller
{
    function index()
    {

        $this->load->model('Gallery_model');
        if ($this->input->post('upload'))
        {
            $this->Gallery_model->do_upload();

        }
        $data['images'] = $this->Gallery_model->get_images();
        $this->load->view('gallery', $data);
    }   
}

`

模型 `

<?php
class Gallery_model extends CI_Model
{

    var $gallery_path;
    var $gallery_path_url;

    function __construct() 
    {
        parent::__construct();

        $this->gallery_path = realpath(APPPATH . '../images');
        $this->gallery_path_url = base_url().'images/';
    }

    function do_upload()
    {

        $config = array(
                'allowed_types' => 'jpg|jpeg|gif|png',
                'upload_path' => $this->gallery_path,
                'max_size' => 2000

            );  


        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $image_data = $this->upload->data();

        $config = array(

                'source_image' => $image_data['full_path'],
                'new_image' => $this->gallery_path . '/thumbs',
                'width'=>150,
                'height'=>100,
                'maintain_ratio'=>true

            );

            $new_image_insert_data = array(
            'img_path' => $this->input->post('userfile'),
            'img_name' => $this->input->post('img_name')
            );
            $insert = $this->db->insert('gallery', $new_image_insert_data);
            return $insert;

        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
        redirect('gallery');
    }



    function get_images()
    {
        $files = scandir($this->gallery_path);
        $files = array_diff($files, array('.', '..', 'thumbs'));

        $images = array();

        foreach ($files as $file) {
            $images []= array (
                    'url' => $this->gallery_path_url . $file,
                    'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file
                );
        }
        return $images;
    }
}

查看

<?php $this->load->view('includes/header'); ?>



    <div id="gallery">
        <?php if(isset($images) && count($images)): 
            foreach($images as $image): ?>

            <div class="thumb">
                <a href="<?php echo $image['url']; ?>">
                    <img src="<?php echo $image['thumb_url']; ?>" />
                </a>

            </div>
        <?php endforeach; else: ?>
            <div id="blank_gallery">Please Upload an Image</div>
        <?php endif; ?>
    </div>

            <div id="page_content">
                <div id="page_content_inner upload">
                    <?php echo form_open_multipart('gallery'); ?>
                        <div class="md-card">
                            <div class="md-card-content">
                                <div class="uk-grid" data-uk-grid-margin>
                                    <div class="uk-width-medium-1-2">
                                        <div class="uk-form-row">
                                            <label for="fullname">Select Image<span class="req"></span></label><br>
                                          <input type="file" id="userfle" name="userfle" required class="md-input" />
                                        </div>
                                        <div class="uk-form-row">
                                            <label for="fullname">Image Title<span class="req"></span></label>
                                            <input type="text" id="img_name" name="img_name" required class="md-input" />
                                        </div>
                                        <div class="uk-form-row">
                                            <input type="submit" name="upload" required class="md-input" />
                                        </div>
                                    </div>                            
                                </div>
                            </div>
                        </div>
                    <? echo form_close();?>
                </div>
            </div>

<?php $this->load->view('includes/footer'); ?>

我努力解决这个问题,并让你们了解我的问题是什么?

3 个答案:

答案 0 :(得分:1)

你走错路

    $new_image_insert_data = array(
    'img_path' => $this->input->post('userfile'),
    'img_name' => $this->input->post('img_name')
    );

文件输入不能这样。

上传文件后,您将获得上传文件的所有数据 所以你可以像这样存储

$new_image_insert_data = array(
        'img_path' => $image_data['file_path'],
        'img_name' => $image_data['file_name']
        );

请参阅此link

答案 1 :(得分:0)

上传代码应移至控制器。模型仅适用于数据库

这是错误的输入

$new_image_insert_data = array(
   'img_path' => $this->input->post('userfile'), # Wrong
   'img_name' => $this->input->post('img_name')
);

应添加此$this->gallery_path

$new_image_insert_data = array(
   'img_path' => $this->gallery_path,
   'img_name' => $this->input->post('img_name')
);

$new_image_insert_data = array(
   'img_path' => $image_data['file_path'],
   'img_name' => $image_data['file_name']
);

所以在db上,记录将是

img_path -->../images'   
img_name --> xyz.jpg

答案 2 :(得分:0)

在表单中,您有

之类的输入字段
 <input type="file" id="userfle" name="userfle" required class="md-input" />

将其更改为

<input type="file" id="userfle" name="userfile" required class="md-input" />

在模型中,您使用用户文件 userfle

 $new_image_insert_data = array(
        'img_path' => $this->input->post('userfile'),
        'img_name' => $this->input->post('img_name')
        );

img_path应该是

的文件夹路径
$new_image_insert_data = array(
        'img_path' => $this->gallery_path_url,
        'img_name' => $this->input->post('img_name') /** if you don't want to change uploaded image file name**/
        );