Php – CodeIgniter Image Upload Not Working and No Errors

codeigniterfile-uploadphp

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

Best Solution

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.

Related Question