Tariq Ali
Tariq Ali

Reputation: 35

Codeigniter Image path not Storing in Database

Hi friends its my first question on here i hope will make you understand what I want because I am new on codeigniter and also not good in english :p

So my problems is I am trying to upload image using Codeigniter I have two folder 1.(for full image) 2.(for thumnils image) but when i upload its gives me error message

Column 'img_path' cannot be null

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

here is my controller model and view

Controller:

`

<?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);
    }   
}

`

Model `

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

View

<?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'); ?>

I try hard to solve this problem and also to make you guys understand whats my problem is ?

Upvotes: 0

Views: 1794

Answers (3)

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

Upload code should be moved to controller. Model is only for Database

this is wrong input

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

should be add this $this->gallery_path

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

or

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

So on db, record will be

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

Upvotes: 0

khushi
khushi

Reputation: 178

You are going wrong way

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

File input can't get like this.

After Upload file you will get all data of uploaded file So you can store like this

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

See this link

Upvotes: 1

shafiq.rst
shafiq.rst

Reputation: 1296

In your form you have input field like

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

change it to

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

as in model you are using userfile Not userfle.

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

And img_path should be your folder path with

$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**/
        );

Upvotes: 0

Related Questions