Welcome Guest, Not a member yet? Register   Sign In
Upload Images(image path) into Database
#1

[eluser]HSKrustofsky[/eluser]
I need to be able to upload an image and either store the image into a database, or store it's path into a database, so I can call on the ID to view it. I have been looking around, and can't seem to find anything to work. I especially can't find something that will fit into my existing code. IF anyone could help, that would be great.

Here is the code I have now:

Controller:
Code:
<?php

class Admin extends Controller {
    
    function Admin() {
        parent::Controller();
    }
    
    function index() {
    
        $data = array();
    
        if($query = $this->admin_model->retrieve()) {
            $data['records'] = $query;
        }
        
        $data['title'] = "Admin";
        $this->load->view('panelview', $data);

    }

    function create() {

        if ($_POST) {
            $save = array(
                'title'  => $this->input->post('title'),
                'contents'  => $this->input->post('contents')
            );
            
            $this->admin_model->create($save);
        }
        
        $data['title'] = "Add News";
        $this->load->view('addnews', $data);
        
        if($_POST != NULL) {
            redirect('../admin/');
        }

    }

    function delete() {
        
        $this->admin_model->delete();
        $this->index();

    }

    function update($id) {
        
        if ($_POST) {
            $save = array(
                'title'  => $this->input->post('title'),
                'contents'  => $this->input->post('contents')
            );
            $this->admin_model->update($save, $id);
        }
        
        $data['news'] = $this->admin_model->get($id);
        $data['title'] = "Update";
        $this->load->view('editnews', $data);

        if($_POST != NULL) {
            redirect('../admin/');
        }
        
    }

}

Model:
Code:
<?php

class Admin_model extends Model {
    
    function Admin() {
        parent::Model();
    }
    
    function create($data) {

        $this->db->insert('data', $data);

    }

    function retrieve() {
        
        $this->db->order_by("id", "desc");
        $query = $this->db->get('data');
        return $query->result();

    }

    function update($data, $id) {

        $this->db->where('id', $id);
        $this->db->update('data', $data);

    }

    function delete() {

        $this->db->where('id', $this->uri->segment(3));
        $this->db->delete('data');

    }
    
    
    function get($id) {
        
      return $this->db->get_where('data', array('id' => $id))->row();
    
    }
    
}

View:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset-UTF-8"&gt;
        
        &lt;style type="text/css"&gt;
            body {font-family: sans-serif;}
            #form {width: 675px; margin: auto; border: 2px solid #000; padding: 10px;}
        &lt;/style&gt;
        
    &lt;/head&gt;    
    &lt;body&gt;
        <h1 align="center">Add News</h1>
        <div id="form">
            &lt;?php
                echo form_open_multipart('../admin/create');
                    echo "<label>Title:</label><br />";
                    echo form_input('title') . "<br /><br />";      
            
                    echo "<label>Image:</label><br />";
                    echo form_upload('userfile') . "<br /><br />";
            
                    echo "<label>Content:</table><br />";
                    echo form_textarea('contents') . "<br /><br />";
                    
                    echo form_submit('', 'Submit');
                echo form_close();
            ?&gt;
        </div>
    &lt;/body&gt;
&lt;/html&gt;

I have already created row in my database table named img_path in order to upload the image path. If someone could help, that would be greatly appreated. THANKS.
#2

[eluser]n0xie[/eluser]
Take a look here. Or try to access the global $_FILES.
#3

[eluser]HSKrustofsky[/eluser]
Okay, I was able to get an image to upload to a folder, but I am unable to save the image path, or name, to store(save) into my database. The column in my database is called img_path. Any assistance would be awesome.

Here is my controller:
Code:
function create() {

        if ($_POST) {
            $save = array(
                'title'  => $this->input->post('title'),
                'contents'  => $this->input->post('contents')
            );
            
            $config = array(
                'upload_path' => 'uploads/',
                'allowed_types' => 'gif|jpeg|jpg|png',
                'max_size' => '5120' // 5MB
            );

            $this->load->library('upload', $config);
            $this->upload->initialize($config);

            if ($this->upload->do_upload()) {
                $data = array(
                    'upload_data' => $this->upload->data()
                );
                
                $file_name = $data['upload_data']['file_name'];
                
                $data = array(
                    'img_path' => $file_name
                );
            }
            
            $this->admin_model->create($save, $data);
        }
        
        $data['title'] = "Add News";
        $this->load->view('addnews', $data);
        
        if($_POST != NULL) {
            redirect('../admin/');
        }

    }
#4

[eluser]HSKrustofsky[/eluser]
Anyone? Sad
#5

[eluser]danmontgomery[/eluser]
Your function definition only accepts one parameter:

Code:
function create($data) {

        $this->db->insert('data', $data);

    }

You're passing it two:

Code:
$this->admin_model->create($save, $data);

So the second one is ignored. You should be appending the img_path value onto the first array, not creating a new one.

Code:
$file_name = $data['upload_data']['file_name'];
$save['img_path'] = $file_name;
#6

[eluser]HSKrustofsky[/eluser]
You are a genius! It worked, thank you.




Theme © iAndrew 2016 - Forum software by © MyBB