Search code examples
phpcodeigniterfile-uploadmod-security

uploading multiple pictures causeing 500 error


I modified a script i found on here to do process multiple pictures being uploaded at once. However when I try to run the script it throws an error. I use to have the script only allow one picture upload at time and that worked fine without any issue.

Here is my code.

Function uploadMultiple(){
    $config = array(
        'allowed_types' => 'jpg|png|jpeg|gif',
        'upload_path' => $this->board_path,
        'overwrite' => false,
        //'file_name' => $fileName

    );
    //print_r($config);

    $this->load->library('upload');
    $errorCount = 0;
    $results = array(
        'errorsPresent' => false,
    );
    $successCount = 0;

    //for each image...try to upload.  if it fails, add it to the error list.
    //keep a list of successful uploads.
    print_r($_FILES);
    for ($i = 0; $i<count($_FILES); $i++){
        echo 'here';
        $_FILES['userfile']['name']    = $_FILES['userfile' . $i]['name'];
        $_FILES['userfile']['type']    = $_FILES['userfile' . $i]['type'];
        $_FILES['userfile']['tmp_name'] = $_FILES['userfile' . $i]['tmp_name'];
        $_FILES['userfile']['error']       = $_FILES['userfile' . $i]['error'];
        $_FILES['userfile']['size']    = $_FILES['userfile' . $i]['size'];

        $config['file_name']     = 'img_' . time() . '.png'; //inserts the unix time into the file name.
        $config['upload_path']   = $this->board_path;
        $config['allowed_types'] = 'jpg|jpeg|gif|png';
        $config['max_size']      = '0';
        $config['overwrite']     = FALSE;
        $this->upload->initialize($config);

        if ( ! $this->upload->do_upload()){
            $results['errorsPresent'] = true;
            $results['error'][$errorCount] = $this->upload->display_errors();
            $errorCount ++;

        } else {
            $data = array('upload_data' => $this->upload->data());              
            $pictureData = $this->upload->data();
            $file_location = $pictureData['full_path'];
            $file_location = substr($file_location, 18);//this should probably be dynamic...
            $file_location = $this->db->escape($file_location);
            $results['success'][$successCount] = $file_location;

            chmod($pictureData['full_path'], 777); //don't need to give it execute permissions but oh well.
            $successCount ++;       
        }

    }

    return $results;
}

Here is the 500 error.

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

This is what the apache log file says:

[Wed Mar 23 02:29:41 2011] [error] [client 129.21.129.32] ModSecurity: Access denied with code 500 (phase 4). Pattern match "(?:\b(?:(?:s(?:elect list because it is not contained in (?:an aggregate function and there is no|either an aggregate function or the) GROUP BY clause|upplied argument is not a valid (?:(?:M(?:S |y)|Postgre)SQL|O(?:racle|DBC)))|S(?:yntax error converti ..." at RESPONSE_BODY. [file "/etc/apache2/conf.d/modsecurity/modsecurity_crs_50_outbound.conf"] [line "23"] [id "970003"] [msg "SQL Information Leakage"] [severity "WARNING"] [tag "LEAKAGE/ERRORS"] [hostname "hostname.com"] [uri "/longboard/index.php/board/add"] [unique_id "TYmTVYEVgWYAAASKoIcAAAAJ"]

Based on the error message I think modsecurity is blocking the script for some reason but i'm not sure why. Any insight would be greatly appreciated.

Thanks


Solution

  • It ended up being a database error. Mod_security was blocking the error message. I went into the mod_security log file and found which rule was causing it to throw the 500 error. I then went into the file with that rule and commented it out. I restarted apache and retested and then the database error showed. I'm thinking of leaving this rule commented out since this is a development server. (It does broadcast to the whole world though, and the reason I have Mod_security installed.)