GluePear
GluePear

Reputation: 7735

Codeigniter - two upload fields in one form

I'm trying to upload an image and an mp3 in Codeigniter, from a form with two file input fields. The image field is called userfile, the mp3 field is called trackfile.

In my model I have this code (modified):

        $image_resource = $this->library_model->upload_image($_FILES);
        $data = array(
            'name' => $this->input->post('name'),
            'imageresource' => $image_resource
            );          

        $this->db->insert('table_name',$data);
        $insert_id = $this->db->insert_id();
        if ($_FILES && $_FILES['trackfile']['name'] !== "") {
            $config = array(
                'allowed_types' => 'mp3',
                'upload_path' => './assets/samples/'
            );
            $this->load->library('upload',$config);
            //echo 'about to upload';
            if ($this->upload->do_upload('trackfile')) {
                $file_data = $this->upload->data();
                //echo 'uploaded: <pre>'; print_r($file_data); echo '</pre>'; die();
                $track_data['trackfile'] = $file_data['file_name'];
                $this->db->where('id', $insert_id);
                return $this->db->update('table_name',$track_data);
            } else {
                echo 'Failed: '; 
                echo $this->upload->display_errors();   
                echo 'FILE DATA: <pre>'; print_r($_FILES['trackfile']); echo '</pre>';
            } 
        }

My problem is that this produces the error "The filetype you are attempting to upload is not allowed." I've checked the mp3 type, it's audio/mpeg, which is in the mimes.php file.

This only happens when I'm also uploading an image. If I leave the image field blank, the mp3 uploads fine. Any help much appreciated.

Upvotes: 1

Views: 2078

Answers (1)

Boris
Boris

Reputation: 802

EDIT

According to the CI docs, the allowed_types portion of the config is a pipe delimited list of mime types, not file extensions. For example, mp3 have the mime type 'audio/mpeg'. So your allowed_types config should look like this:

$config = array(
            'allowed_types' => 'audo/mpeg|audio/x-wav|audio/x-aiff|application/ogg',
            'upload_path' => './assets/samples/'
           );

If that doesn't work try to initialize the library as mentioned here.

Upvotes: 1

Related Questions