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!
#2

[eluser]Cristian Gilè[/eluser]
Hi sb05,

from codeigniter guide:

Quote:Create a folder at the root of your CodeIgniter installation called uploads and set its file permissions to 777.

Uploads folder should be in the root not inside application.

Cristian Gilè
#3

[eluser]sb05[/eluser]
Yes I forgot to tell that. I already did that, and it also didn't work Sad
#4

[eluser]Cristian Gilè[/eluser]
To define $full_file_path you can do this:

Code:
$full_file_path = $upload_data['full_path'];

but this is not the issue.


Are you sure the uploads folder is in the right place? Uploads folder should be at the same level of index.php file and check for 777 permissions.

Cristian Gilè
#5

[eluser]sb05[/eluser]
Yes, I did. I actually double checked again. By the way I'm doing this on the mac. So I changed the permissions again to the uploads folder to 777. And it again says it has all the writing permissions. So I am really confused.

This is driving me nuts. This app has to be ready in a couple of days for school, but nobody there knows a thing about CodeIgniterSad
I have double checked everything, and I really can't see it.
#6

[eluser]Unknown[/eluser]
What kind of server software are you using? Running locally? I had problems with permissions on MAMP (for mac). I use BatchMod to set my permissions now. It works better. If you run it locally: Make sure that the server has the permissions of 777 and not"you" as the OS user.
#7

[eluser]Cristian Gilè[/eluser]
By default the upload routine expects the file to come from a form field called userfile, and the form must be a "multipart type.

Change your form tag to:
Code:
&lt;form method="post" action="&lt;?php echo $action; ?&gt; enctype="multipart/form-data" /&gt;

and change your input file to:
Code:
&lt;input type="file" name="userfile" class="text" value="&lt;?php echo $this-&gt;validation->image_url; ?&gt;"/>

Cristian Gilè
#8

[eluser]sb05[/eluser]
Sorry for the late response guys, I was messing with my code here and there and it got all spaghetti all of a sudden Tongue

@Algoritm
Yes apparently MAMP was the problem that the image did not upload. When I ran it on my remote server, the uploading went well. However only the image went to my folder but the other textfields did not go to my database. But Cristian solved that with his last postSmile

@cristian
I already tried the enctype once but it also didnt work. However in the input type name="" . I had image_url instead of userfile. But now with the enctype AND name="userfile", it worked!

So thank you guys very much for the help:-D




Theme © iAndrew 2016 - Forum software by © MyBB