Welcome Guest, Not a member yet? Register   Sign In
How to add image upload to my CRUD
#1

[eluser]sb05[/eluser]
Hi Guys,

I am really struggling adding an image upload in my form. WIth this code below I get an error: undefined variable full_file_path. But I did define it, so I don't know what's wrong. Hope you guys can help out.

Here is my controller: articles.php
Code:
function add(){
        // set validation properties
        $this->_set_fields();
        
        // set common properties
        $data['title'] = 'Add new Article';
        $data['message'] = '';
        $data['action'] = site_url('articles/addArticle');
        $data['link_back'] = anchor('articles','Back to list of News Articles',array('class'=>'back'));
    
        // load view
            $this->load->view('banner_view', $data);
            $this->load->view('left_column_view', $data);
            $this->load->view('right_column_view', $data);
            $this->load->view('articleEdit', $data);
            $this->load->view('footer_view', $data);    }
    
    function addArticle(){
        // set common properties
        $data['title'] = 'Add new Article';
        $data['action'] = site_url('articles/addArticle');
        $data['link_back'] = anchor('articles','Back to list of Articles',array('class'=>'back'));
        
        // set validation properties
        $this->_set_fields();
        $this->_set_rules();
        
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
            $config['upload_path'] = './uploads';
            $this->load->library('upload', $config);
            

        //    $path_to_uploads='./application/uploads';
            
            
        //    $config['upload_path'] = $path_to_uploads;
                $this->load->library('upload', $config);

                       if (!$this->upload->do_upload()){
                            $error = $this->upload->display_errors();
                            echo "The uploading is not working";
                        }else{
                            $upload_data=$this->upload->data();
                            $file_name=$upload_data['file_name'];
                            $full_file_path = $path_to_uploads.'/'.$file_name;
                        }
        
        // run validation
        if ($this->validation->run() == FALSE){
            $data['message'] = '';
        }else{
            // save data
            $article = array('header' => $this->input->post('header'),
                            'body' => $this->input->post('body'),
                            'image_url' => $full_file_path)
                         ;
            $article_id = $this->articleModel->save($article);
            
            // set form input name="id"
            $this->validation->article_id = $article_id;
            
            // set user message
            $data['message'] = '<div class="success">add new ARTICLE success</div>';
        }
        
        // load viewinclude 'person.php';
        
            $this->load->view('banner_view', $data);
            $this->load->view('left_column_view', $data);
            $this->load->view('right_column_view', $data);
            $this->load->view('articleEdit', $data);
            $this->load->view('footer_view', $data);    }

And my view: articleEdit.php
Code:
&lt;form method="post"   action="&lt;?php echo $action; ?&gt;"&gt;
        <div id="p">
        <table>
            <tr>
                <td width="30%">ID</td>
                <td>&lt;input type="text" name="id" disabled="disable" class="text" value="&lt;?php echo $this-&gt;validation->article_id; ?&gt;"/></td>
                &lt;input type="hidden" name="article_id" value="&lt;?php echo $this-&gt;validation->article_id; ?&gt;"/>
            </tr>
            <tr>
                <td valign="top">Header<span style="color:red;">*</span></td>
                <td>&lt;input type="text" name="header" class="text" value="&lt;?php echo $this-&gt;validation->header; ?&gt;"/>
                &lt;?php echo $this->validation->header_error; ?&gt;</td>
            </tr>
                 <td valign="top">Body<span style="color:red;">*</span></td>
                <td>&lt;input type="textarea" name="body" class="text" value="&lt;?php echo $this-&gt;validation->body; ?&gt;"/>
            &lt;?php echo $this->validation->body_error; ?&gt;</td>
              
            </tr>
                <tr>
                    <td valign="top">Image_url<span style="color:red;">*</span></td>
                    <td>&lt;input type="file" name="image_url" class="text" value="&lt;?php echo $this-&gt;validation->image_url; ?&gt;"/>
                    &lt;?php echo $this->validation->image_url_error; ?&gt;</td>
                </tr>
            <tr>
                <td>&nbsp;</td>
                <td>&lt;input type="submit" value="Save"/&gt;&lt;/td>
            </tr>
        </table>
        </div>&lt;!-- END P--&gt;
        &lt;/form&gt;

My database is working. Without the file upload everything works fine, and when I add the file upload code, the only thing my code does is store the name of the image in the image_url field in the database. But the image does not get uploaded in my uploads folder.

Hope you guys can help!


Messages In This Thread
How to add image upload to my CRUD - by El Forum - 01-09-2011, 10:16 AM
How to add image upload to my CRUD - by El Forum - 01-09-2011, 10:26 AM
How to add image upload to my CRUD - by El Forum - 01-09-2011, 10:31 AM
How to add image upload to my CRUD - by El Forum - 01-09-2011, 10:45 AM
How to add image upload to my CRUD - by El Forum - 01-09-2011, 11:42 AM
How to add image upload to my CRUD - by El Forum - 01-09-2011, 08:19 PM
How to add image upload to my CRUD - by El Forum - 01-09-2011, 08:38 PM
How to add image upload to my CRUD - by El Forum - 01-12-2011, 05:50 AM



Theme © iAndrew 2016 - Forum software by © MyBB