CodeIgniter Forums
Solved: insert pic - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Solved: insert pic (/showthread.php?tid=70564)



Solved: insert pic - davy_yg - 04-27-2018

Hello,

I am trying to insert pic file name into the database.  How to do that?


controllers/upload_image.php


PHP Code:
 public function do_upload()
        {
                $config['upload_path']          './uploads/';
                $config['allowed_types']        'gif|jpg|png';
                $config['max_size']             100;
                $config['max_width']            500;
                $config['max_height']           400;

                $this->load->library('upload'$config);
                $this->load->model('mphoto');

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

                        $this->load->view('upload'$error);
                }
                else
                {
                        $data = array('upload_data' => $this->upload->data());

                        $this->mphoto->insert_pic();

                        $this->load->view('upload_success'$data);
                }
        




models/mphoto.php

PHP Code:
    class pic_model extends CI_Model {

        public $title;
        public $content;
        public $date;

        public function get_last_two_entries()
        {
                $query $this->db->get('photos'2);
                return $query->result();
        }

        public function insert_pic()
        {
                $this->pic_name    $_FILE['userfile']; // please read the below note
                
                $this
->db->insert('photos'$this);         
        } 
       

I am not sure if this is correct: 
PHP Code:
$this->pic_name    $_FILE['userfile']; 

I simply want to capture the file name.


RE: insert pic - InsiteFX - 04-27-2018

Read the User Guide!

PHP Code:
// full_path gives you the path and file name
$this->pic_name $this->upload->data('full_path'); 


Upload Data


RE: insert pic - davy_yg - 04-28-2018

I wonder why I cannot load the model:

An uncaught Exception was encountered

Type: RuntimeException

Message: C:\xampp\htdocs\ci_upload_image\application\models/Mphoto.php exists, but doesn't declare class Mphoto

Filename: C:\xampp\htdocs\ci_upload_image\system\core\Loader.php

Line Number: 340

Backtrace:

File: C:\xampp\htdocs\ci_upload_image\application\controllers\upload_image.php
Line: 13
Function: model

File: C:\xampp\htdocs\ci_upload_image\index.php
Line: 315
Function: require_once


RE: insert pic - davy_yg - 04-28-2018

When I am trying to get the filename value from the model it is null.


A Database Error Occurred

Error Number: 1048

Column 'pic_name' cannot be null

INSERT INTO `photos` (`pic_name`) VALUES (NULL)

Filename: C:/xampp/htdocs/ci_upload_image/system/database/DB_driver.php

Line Number: 691

mphoto.php

public function insert_pic()
{

$this->pic_name = $this->upload->data('filename');

$this->db->insert('photos', $this);
}

cupload_image.php

public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 500;
$config['max_height'] = 400;

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


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

$this->load->view('upload', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());

$this->mphoto->insert_pic();

$this->load->view('upload_success', $data);
}
}


RE: insert pic - davy_yg - 04-28-2018

Nevermind, I already find the answer to this thread. I think I am going to close it. Thanks!