簡體   English   中英

Codeigniter映像路徑未存儲在數據庫中

[英]Codeigniter Image path not Storing in Database

嗨,朋友們,這是我的第一個問題,希望您能理解我的意思,因為我是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'); ?>

我盡力解決這個問題,也使你們了解我的問題是什么?

你走錯路了

    $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']
        );

看到這個鏈接

上傳代碼應移至控制器。 模型僅適用於數據庫

這是錯誤的輸入

$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']
);

所以在數據庫上,記錄將是

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

在您的表單中,您有如下輸入字段

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

更改為

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

與模型中一樣,您正在使用userfile而不是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**/
        );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM