Optional File Upload Field - Codeigniter

Codeigniter has a great file upload library to handle user submitted images and more via HTML forms. Using the File Uploading Class, you can easily manipulate, restrict and handle user content. What the class does not handle well, files as optional fields. Codeigniter does not provide any baked in methods to check if the user is attempting to pass a file to upload, before you attempt to upload it. Also, because the field name of a file field is not passed the same way as other input fields, you are not able to check if it exists in the traditional way.
// Doesn't work on file form fields 
if ($this->input->post("user-image"))
Meaning we cannot independently verify the existence of the file via the input class, and Codeigniter's file upload class does not give a way to check if the file exists before calling $this->upload->do_upload();. How do we try to upload a file, without showing users an error when they didn't supply an optional file?

How to check if a file field is selected to upload

Option A: Check reason for upload failure before displaying error. Codeigniter doesn't provide an error code for failed uploads, but it does provide an error message.
$this->load->library("upload");
if (!$this->upload->do_upload()) { 
    // Our upload failed, but before we throw an error, learn why
    if ("You did not select a file to upload." != $this->upload->display_errors('','')) {
        // in here we know they DID provide a file
        // but it failed upload, display error
        $upload_error = array('error' => $this->upload->display_errors());
$this->load->view("my_upload_form", $upload_error);
    }
    else {
        // here we failed b/c they did not provide a file to upload
        // fail silently, or message user, up to you
    }
}
else {
    // in here is where things went according to plan. 
    //file is uploaded, people are happy
}
Option B: Checking if a file exists from the HTML form upload via raw PHP. The pro of this method is method is that you are checking for the file before using the Codeigniter library to upload it.
if(isset($_FILES['userfile']) && $_FILES['userfile']['size'] > 0){
    // user provided a file to upload
    $this->load->library("upload");
    if (!$this->upload->do_upload()) {
         // already checked for file from form, so this is a legit error
         $upload_error = array('error' => $this->upload->display_errors());
$this->load->view("my_upload_form", $upload_error);
    }
    else {
        // in here is where things went according to plan. 
        // file is uploaded, people are happy 
    }
} 
else {
    // in here means no file was provided to upload
    // choose to ignore or let them know
}
Small dog web development studio

View All Our Posts

Web application developers in western Washington, creating user centric web solutions for businesses of all shapes and sizes.

Our projects include ecommerce, event organization, user driven web applications and more.


Small Dog Studios, LLC. Small Dog Studios News