Welcome Guest, Not a member yet? Register   Sign In
Upload image and save file name to database
#1

[eluser]JaredKC[/eluser]
I am still new to CI and could use a little help. I have a form with a few input text fields and one file field to to upload an image. I have the image uploading and all the other input fields saving to the database, but I have not been able to load the file name and save it to the database. I may be missing something simple, if I am, I hope someone can point it out. Thank you.

Here is my code:

Form from View file
Code:
<?php echo form_open_multipart('upload/do_upload');?>
    <?=form_hidden('project_id', "1");?>
    <fieldset>
        <legend>Add Project Image</legend>
        <p>Title:<br /> &lt;input type="text" name="title" /&gt;&lt;/p>
        <p>Description:<br /> &lt;textarea name="description" rows="3" cols="20"&gt;&lt;/textarea></p>
        <p>File: &lt;input type="file" name="file_name" /&gt;&lt;/p>
        <p>&lt;input type="submit" value="Upload" /&gt;&lt;/p>
    </fieldset>
&lt;/form&gt;


Controller
Code:
&lt;?php
class Projects extends Controller {
    
    function Projects()
    {
        parent::Controller();        
        $this->load->model('Projects_model');      
        $this->load->helper(array('form', 'url'));
    }
    
    function index()
    {    
        $this->load->view('upload_form', array('error' => ' ' ));
    }

    function do_upload()
    {
        $config['upload_path'] = './project_imgs/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1200';
        $config['overwrite']  = FALSE;
        $config['remove_spaces']  = TRUE;
        $field_name = "file_name";
        
        $this->load->library('upload', $config);
    
        if ( ! $this->upload->do_upload($field_name))
        {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('upload_form', $error);
        }    
        else
        {
            $data = array('upload_data' => $this->upload->data());
            $data['query'] = $this->Projects_model->add_image();
            $this->load->view('upload_success', $data);
        }
    }    
}
?&gt;


Model
Code:
&lt;?php
class Projects_model extends Model {
    
    function Projects_model()
    {
        parent::Model();
    }
    
// Add Edit and Delete Project Images
    function add_image()
    {
        $this->db->insert('project_imgs', $_POST);
    }
}
?&gt;
#2

[eluser]JaredKC[/eluser]
Oops! Posted this in the wrong area. Sorry.
#3

[eluser]pistolPete[/eluser]
The filename is not saved in $_POST (which you should not use directly btw --&gt; input class).
You can access the data using:
Code:
$upload_data = $this->upload->data();
echo $upload_data['file_name'];

Have a look at the user guide.
#4

[eluser]JaredKC[/eluser]
Here is my revised code to make it work incase it helps anyone.
Code:
&lt;?php
class Projects extends Controller {
    
    function Projects()
    {
        parent::Controller();        
        $this->load->model('Projects_model');      
        $this->load->helper(array('form', 'url'));
    }
    
    function index()
    {    
        $this->load->view('upload_form', array('error' => ' ' ));
    }

    function do_upload()
    {
        $config['upload_path'] = './project_imgs/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1200';
        $config['overwrite']  = FALSE;
        $config['remove_spaces']  = TRUE;
        $field_name = "file_name";
        
        $this->load->library('upload', $config);
    
        if ( ! $this->upload->do_upload($field_name))
        {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('upload_form', $error);
        }    
        else
        {
             $upload_data = $this->upload->data();
             $file = $upload_data['file_name'];

            $data['query'] = $this->Projects_model->add_image($file);
        }
    }    
}
?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB