开发者

Codeigniter multiple upload of different file-types

开发者 https://www.devze.com 2023-01-29 20:42 出处:网络
I want to upload an audio file and an image in the same form and store their names in the db...what happens is only one of the files is uploaded (the one that is first in row)

I want to upload an audio file and an image in the same form and store their names in the db...what happens is only one of the files is uploaded (the one that is first in row) the other gives an error of not allowed type

here is my view:

        <?php echo form_open_multipart('index.php/ena_dyo_edit');?>
        Title:<input type='text' name="title" size="40" id="title" />
        <p>Title Photo:<input type="file" name="title_photo" size="20" id="title_photo" /></p>
        <p>Body:<textarea name="body" rows = "10" cols="60" id="body"></textarea><p>
        <p>Soundfile:<input type="file" name="soundfile" size="20" id="soundfile" /></p>
        <p>Author:<input type="text" name="author" size="40" id="author"/></p>
        <p><input type="submit" value="Submit New Post"/></p>
        <?php echo form_close(); ?>

and here are my model functions:

function soundfile_upload($userfile = 'userfile'){
    //uploads a sound file

     $config = array(
            'upload_path' => './uploads/soundfiles/',
            'allowed_types' => 'mp3|wav|aif|aiff|ogg',
            'max_size' => '256000' //250 MB max size
        );


      $this->load->library('upload', $config);

       if (!$this->upload->do_upload($userfile)) {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_error', $error);
        }

      else{

           $soundfile_data = $this->upload->data();
           $entry_title = $this->input->post('title'); //using the name of the post to relate the audio file
           $entry_id = $this->db->insert_id(); //the id of our last entry!

           //create array to load to database
           $insert_data = array(
                'soundfile' => $soundfile_data['file_name']
               );

            //$this->db->where('id', $entry_id);
            $this->db->update('entries', $insert_data,array('id' => $entry_id));  //load array开发者_运维问答 to database
      }



  }

  function title_photo_upload($userfile = 'userfile'){
    //uploads a photo for a post title

     $config = array(
            'upload_path' => './uploads/title_photos/',
            'allowed_types' => 'jpg|jpeg|gif|png',
            'max_size' => '256000' //250 MB max size
        );


      $this->load->library('upload', $config);

       if (!$this->upload->do_upload($userfile)) {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_error', $error);
        }

      else{

           $soundfile_data = $this->upload->data();
           $entry_id = $this->db->insert_id(); //the id of our last entry!

           //create array to load to database
           $insert_data = array(
                'title_photo' => $soundfile_data['file_name']
               );


            $this->db->update('entries', $insert_data,array('id' => $entry_id));  //load picture where id = entry_id



  }
}


According to the CI docs, the 'allowed_types' portion of the config is a pipe delimited list of mime types, not file extensions. It looks to me like you're using the file extensions which would probably produce an error of not allowed type if the extension isn't the same as the mime type. For example, my mp3 files tend to have the mime type 'audio/mpeg'. So your allowed_types config should look like this:

'allowed_types' => 'audo/mpeg|audio/x-wav|audio/x-aiff|application/ogg'
0

精彩评论

暂无评论...
验证码 换一张
取 消