Welcome Guest, Not a member yet? Register   Sign In
Help: problem in file uploading
#1

[eluser]Wondering Coder[/eluser]
hi., really badly need help.

I have a controller, model and view for saving image name in my db. In my other site this work but somehow now it won't. I'm rewriting my code to a more order way so other programmers may easily be able to understand or edit my code.

My Controller function for upload
Code:
function upload_photo()
    {
        $data['mod'] = strtolower(get_class($this));
        $data['role'] = $this->session->userdata('data_id');    
        $dt = $this->session->userdata('user_id');
        $data['user'] = $this->main->getProfile($dt)->row();
        $data['error'] = '';
        
        $config['upload_path'] = './img/pics/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        
        $this->load->library('upload', $config);
              
        if($this->form_validation->run('update')===FALSE)
        {
            $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
        }
        else
        {
            if ( ! $this->upload->do_upload())
            {          
                $data['error'] = $this->upload->display_errors('<p class="error">','</p>');
            }    
            else
            {
                $data2 = array('upload_data' => $this->upload->data());              
                $file_data = $this->upload->data();
                $file_name = $file_data['file_name'];
                
                $detail = array(
                        'image_path' =>$file_name
                        
                    );
                    echo $file_name;
                $this->main->save($detail, $dt);
                //redirect('student_profile/update');
            }
        }  
        
        $data['content'] = $this->load->view('panes/upload_pic', $data, TRUE);
        $this->output->enable_profiler(TRUE);
        $this->load->vars($data);
        $this->load->view('template');        
    }

My model in saving
Code:
function save($data, $item)
    {
        if ($item=='')
            {
            $this->db->trans_start();
            $this->db->insert('users', $data);
            $item = $this->db->insert_id();
                if($data['data_id']==1)
                {
                    $this->save_detail($item);
                }
            $this->db->trans_complete();                    
            }
        else
            {
            $this->db->trans_start();
            $this->db->where('a.user_id', $item);
            $this->db->update('users spms_a', $data);
            $this->update_detail($item);    
            $this->db->trans_complete();
            }
            
        return $item;    
    }

then my view
Code:
<div class="contentpane">
    <div class="guestBookcontainer" style="padding-left:10px; ">
    <div class="sectortitle" style="padding-left:8px">Upload Profile Picture</div>&nbsp;
       <p class="response">&nbsp;</p>
       <div style="padding-left:30px">
      
            &lt;form method="post" action="&lt;?=base_url();?&gt;student_profile/upload_photo" enctype="multipart/form-data" &gt;&nbsp;
                <div class="rleft">Picture</div>
                    &lt;input type="file" name="userfile" /&gt;&lt;?php echo $error;?&gt;                                                                                                    
                    &lt;input type="submit" value="Save" name="submit"&gt;    
                
            &lt;/form&gt;
      </div>

    </div>
</div>
This should work but it won't. grrr. Even my form validation errors is not returning anything. BTW - I'm using MY_Form_validation in application/libraries and form_validation config file and all this work in my previous site in CI 2.0.

PS: My recent changes in my site is I'm now using MY_Controller to extend CI_Controller.
#2

[eluser]InsiteFX[/eluser]
Did you place your MY_Controller in application/core ?

InsiteFX
#3

[eluser]Wondering Coder[/eluser]
got it working now. My problem was my validation. Even when i pass data's it will always return false coz the logic isn't right but i fixed it now.

This is the result of copy paste, hehe.




Theme © iAndrew 2016 - Forum software by © MyBB