Welcome Guest, Not a member yet? Register   Sign In
Form Data and File Upload
#14

[eluser]wowdezign[/eluser]
Okay. I got a solution. There are probably other solutions, but I can explain why it was failing for me.

The upload field gets put into $_FILES even if it is empty and the error and size are set to 4 and 0 respectively Here is the do_upload function from CI:
Code:
function do_upload($field = 'userfile'){
    // Is $_FILES[$field] set? If not, no reason to continue.
    if ( ! isset($_FILES[$field])){
        $this->set_error('upload_no_file_selected');
        return FALSE;
    }
    // Is the upload path valid?
    if ( ! $this->validate_upload_path()){
        // errors will already be set by validate_upload_path() so just return FALSE
        return FALSE;
    }
    // Was the file able to be uploaded? If not, determine the reason why.
    if ( ! is_uploaded_file($_FILES[$field]['tmp_name'])){
        $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];
        switch($error){
            case 1:    // UPLOAD_ERR_INI_SIZE
                $this->set_error('upload_file_exceeds_limit');
                break;
            case 2: // UPLOAD_ERR_FORM_SIZE
                $this->set_error('upload_file_exceeds_form_limit');
                break;
            case 3: // UPLOAD_ERR_PARTIAL
               $this->set_error('upload_file_partial');
                break;
            case 4: // UPLOAD_ERR_NO_FILE
               $this->set_error('upload_no_file_selected');
                break;
            case 6: // UPLOAD_ERR_NO_TMP_DIR
                $this->set_error('upload_no_temp_directory');
                break;
            case 7: // UPLOAD_ERR_CANT_WRITE
                $this->set_error('upload_unable_to_write_file');
                break;
            case 8: // UPLOAD_ERR_EXTENSION
                $this->set_error('upload_stopped_by_extension');
                break;
            default :   $this->set_error('upload_no_file_selected');
                break;
        }
        return FALSE;
    }
When I submitted my form with an empty upload field I got the following for $_FILES:
Code:
Array
(
    [userfile] => Array
        (
            [name] =>
            [type] =>
            [tmp_name] =>
            [error] => 4
            [size] => 0
        )
)
So the solution for me was to do:
Code:
if($_FILES['userfile']['error'] != 4){
  // Do upload here
}
Like I said before. There are probably better ways to do this, but My form is working now. Hopefully I won't find something I overlooked later and get :red:.


Messages In This Thread
Form Data and File Upload - by El Forum - 11-30-2009, 03:48 PM
Form Data and File Upload - by El Forum - 11-30-2009, 06:28 PM
Form Data and File Upload - by El Forum - 11-30-2009, 08:57 PM
Form Data and File Upload - by El Forum - 11-30-2009, 08:58 PM
Form Data and File Upload - by El Forum - 12-01-2009, 01:21 AM
Form Data and File Upload - by El Forum - 12-01-2009, 02:39 AM
Form Data and File Upload - by El Forum - 12-01-2009, 07:34 AM
Form Data and File Upload - by El Forum - 12-01-2009, 07:39 AM
Form Data and File Upload - by El Forum - 12-01-2009, 07:41 AM
Form Data and File Upload - by El Forum - 12-01-2009, 07:43 AM
Form Data and File Upload - by El Forum - 12-01-2009, 07:55 AM
Form Data and File Upload - by El Forum - 12-01-2009, 08:17 AM
Form Data and File Upload - by El Forum - 12-01-2009, 08:22 AM
Form Data and File Upload - by El Forum - 12-01-2009, 08:59 AM
Form Data and File Upload - by El Forum - 12-01-2009, 09:20 AM
Form Data and File Upload - by El Forum - 12-01-2009, 10:57 AM
Form Data and File Upload - by El Forum - 12-01-2009, 11:03 AM
Form Data and File Upload - by El Forum - 12-01-2009, 05:43 PM
Form Data and File Upload - by El Forum - 12-02-2009, 05:15 AM
Form Data and File Upload - by El Forum - 01-21-2010, 12:51 AM
Form Data and File Upload - by El Forum - 01-21-2010, 08:29 AM
Form Data and File Upload - by El Forum - 01-21-2010, 08:15 PM
Form Data and File Upload - by El Forum - 12-07-2010, 06:13 AM
Form Data and File Upload - by El Forum - 12-07-2010, 12:08 PM
Form Data and File Upload - by El Forum - 12-07-2010, 07:55 PM
Form Data and File Upload - by El Forum - 12-08-2010, 11:14 AM
Form Data and File Upload - by El Forum - 10-05-2012, 02:38 AM
Form Data and File Upload - by El Forum - 10-05-2012, 02:54 AM
Form Data and File Upload - by El Forum - 10-07-2012, 06:02 PM
Form Data and File Upload - by El Forum - 10-07-2012, 06:16 PM



Theme © iAndrew 2016 - Forum software by © MyBB