0

Okay so I've been messing with this for a couple hours now, reading the docs and browsing forums but I cannot find out why this upload form isn't working.

The entire form works perfectly, data saves to the DB and everything. But I just added an image upload input and it doesn't work! I have followed the exact tutorial in the docs, as well as several others.

Here is the code that processes the form submit ($this->page_m is my model):

public function edit ($id = NULL)
{
    // Fetch a page or set a new one
    if ($id) {
        $this->data['page'] = $this->page_m->get($id);
        count($this->data['page']) || $this->data['errors'][] = 'page could not be found';
    }
    else {
        $this->data['page'] = $this->page_m->get_new();
    }

    // Pages for dropdown
    $this->data['pages_no_parents'] = $this->page_m->get_no_parents();      

    // Process the form
    if ($this->form_validation->run('page_rules') == TRUE) {
        $data = $this->page_m->array_from_post(array(
            'title', 
            'slug', 
            'body', 
            'template', 
            'parent_id'           
        ));                    

        if(!empty($_FILES['userfile'])) {
           $this->do_upload();
        }

        $this->page_m->save($data, $id); 
        redirect('admin/page');         
    }        

    // Load the view
    $this->data['subview'] = 'admin/page/edit';
    $this->load->view('admin/_layout_main', $this->data);
}

and here is the code that processes the photo upload, which is called right after the $this->form_validation->run() check:

//upload a slider photo
public function do_upload() {                   

     $config = array(
         'allowed_types' => 'jpg|jpeg|gif|png',
         'upload_path' => site_url('uploads/')
     );       


     $this->load->library('upload');
     $this->upload->initialize($config);

     $field_name = "userfile";

     if ( ! $this->upload->do_upload($field_name)) {
             $this->data['error'] = array('error' => $this->upload->display_errors());
             $this->data['subview'] = 'admin/page/edit';
             $this->load->view('admin/_layout_main', $this->data);
     } else {
         return true;
     }                                  
}

I have purposely made this upload script as simple and basic as possible for debugging purposes, but still I have had no success in getting this form to upload properly.

After I get the upload working I need to save the image name in my database table, but that is besides the point right now. I just need to get this actually uploading.

SOLVED -- INVALID UPLOAD DIRECTORY

7
  • Did you add enctype="multipart/form-data" to form ? Commented May 24, 2013 at 16:04
  • Yes, I am opening the form with echo form_open_multipart() which is a codeigniter function to open up a multipart form Commented May 24, 2013 at 16:05
  • does your upload directory have the correct permissions? Commented May 24, 2013 at 16:07
  • 1
    Inside if ( ! $this->upload->do_upload($field_name)) { try adding: echo $this->upload->display_errors();die();. Test the form again and tell us if you get a message. Commented May 24, 2013 at 16:10
  • @Jeemusu I wish I would have done that a while ago haha. It said my upload directory was invalid. I adjusted my upload directory from site_url('uploads/'); to just 'uploads/' and it worked. Thank you very much. Commented May 24, 2013 at 16:13

2 Answers 2

1

To debug your script you should try and echo out the value returned by $this->upload->display_errors();

Try changing your do_upload() methods last if{} else {} statement to the following:

 if ( ! $this->upload->do_upload($field_name)) {

         // return the error message and kill the script
         echo $this->upload->display_errors(); die();

         $this->data['error'] = array('error' => $this->upload->display_errors());
         $this->data['subview'] = 'admin/page/edit';
         $this->load->view('admin/_layout_main', $this->data);
 } else {
     return true;
 } 

Hopefully this will help you find out what is causing the problem.

Sign up to request clarification or add additional context in comments.

Comments

0

same problem with mine, I resolve that by upgrading CodeIgniter,

my old version is 3.1.2, then i just upgrade to CI-3.1.3,

just replace the system folder in root folder,

after that, the problem resolve in live server,

i hope my suggest is useful to use

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.