Problem with update attachment

#1
[eluser]Erwin Setiawan[/eluser]
I have problem with edit my post with it's attachment. this is my scripts on controller

Code:
function edit_post()
    {
        $data['css'] = $this->css;
        $data['base'] = $this->base;
        $data['title'] = "Admin";
        $data['heading'] = "Edit Post";
        $data['message'] = "";
        $data['upload_error'] = "";
        $query=$this->cr_posts_model->get_by_id($this->uri->segment(4));
        foreach($query as $row):
            $data['post_title'] =$row->title;
            $data['body']  =$row->body;
            $data['status']=$row->status;
            $data['attach']=$row->attachment;
        endforeach;
        
        //form post
        $data['input_title'] = array(
                'name'             => 'title',
                'id'             => 'title',
                'maxlength'        =>     '50',
                'size'            =>     '50',
                'style'           => 'width:50%'
             );
        $data['input_body'] = array(
                'name'            => 'body',
                'id'              => 'body',
                'rows'             => '50',
                'cols'             => '600',
                'style'           => 'width:100%'
             );
        $data['input_attachment'] = array(
              'name'         => 'attachment',
              'id'             => 'attachment',
              'maxlength'    =>     '100',
              'size'        =>     '50',
              'type'        => 'file',
              'style'       => 'width:50%'
             );
        $data['post_status'] = array(
                'publish'          => 'Publish',
                'unpublish'        => 'Unpublish',
         );
        $this->form_validation->set_rules('title', 'Title', 'trim|required');
        $this->form_validation->set_rules('body', 'Body', 'trim|required');
        $this->form_validation->set_rules('attachment', 'Attachment', 'trim');
        
        if ($this->form_validation->run() == FALSE || !$this->upload->do_upload('attachment'))
        {
            $data['upload_error'] = array('error' => $this->upload->display_errors());
            $this->load->view('admin/cr_edit_view', $data);
        }
        else //submit success
        {
            //upload
            //$this->upload->do_upload('attachment');
            $data['details'] = $this->upload->data();
            //end upload
            $post_id = $this->cr_posts_model->get_next_id();
            $this->cr_posts_model->update();
            $query=$this->cr_posts_model->get_by_id($this->uri->segment(4));
            foreach($query as $row):
                $data['post_title'] =$row->title;
                $data['body']  =$row->body;
                $data['status']=$row->status;
                $data['attach']=$row->attachment;
            endforeach;
                
            if($_POST['button']=="Save"){
                $data['message'] = "Your post has been saved . Continue editing or <a >base."public/company_report/full_page/".$this->uri->segment(4)."'>View Now</a> ?";
            }
            $this->load->view('admin/cr_edit_view', $data);    
        }
    }


And in my model

Code:
function update()
    {
        $details = $this->upload->data();
        $query=$this->cr_posts_model->get_attach($this->uri->segment(4));
        foreach($query as $row):
            $attach=$row->attachment;
        endforeach;
        if($details['file_name']!=""){
                unlink($this->config->item('upload').str_replace($this->base."files/","",$attach));
                $data = array(
                'title' => $_POST['title'],
                'body' => $_POST['body'],
                'status' => $_POST['status'],
                'attachment' => $this->base."files/cr_attachments/".$details['file_name'],
                'modified' => date("Y-m-d H:i:s"),
                'modified_by' => $this->session->userdata('user_id')  
                );
        }else{
        $data = array(
            'title' => $_POST['title'],
            'body' => $_POST['body'],
            'status' => $_POST['status'],
            'modified' => date("Y-m-d H:i:s"),
            'modified_by' => $this->session->userdata('user_id')  
            );
        }
        $this->db->update('cr_posts',$data, array('post_id' => $this->uri->segment(4)));
    }
It's working, i can delete my file if i change text field attachment and it's replace with new one. But I have problem with empty text field attachment. I want to by pass empty text field attachment and keep post updated with old attachment, but by default code igniters $this->upload->display_errors() always catch error "You did not select a file to upload."

any solution for this?
thx for help^^

#2
[eluser]Ignacio[/eluser]
I'm not sure, but I think that form_validation won't work with type="file" fields.

#3
[eluser]Ignacio[/eluser]
I'm not sure, but I think that form_validation won't work with type="file" fields.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.