【问题标题】:Codeigniter multiple upload of different file-typesCodeigniter 多次上传不同的文件类型
【发布时间】:2010-12-11 13:37:26
【问题描述】:

我想以相同的形式上传音频文件和图像并将它们的名称存储在数据库中...发生的情况是仅上传了一个文件(排在第一位的文件) 另一个给出了不允许类型的错误

这是我的看法:

        <?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(); ?>

这是我的模型函数:

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



  }
}

【问题讨论】:

    标签: php codeigniter file-upload


    【解决方案1】:

    根据 CI 文档,配置的“allowed_types”部分是一个以竖线分隔的 mime 类型列表,而不是文件扩展名。在我看来,您正在使用文件扩展名,如果扩展名与 mime 类型不同,则可能会产生不允许类型的错误。例如,我的 mp3 文件往往具有 mime 类型“audio/mpeg”。所以你的 allowed_types 配置应该是这样的:

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

    【讨论】:

      猜你喜欢
      • 2018-05-29
      • 2013-01-29
      • 1970-01-01
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-08
      相关资源
      最近更新 更多