在codeigniter中上传不同文件类型的问题

时间:2010-05-11 16:40:28

标签: php codeigniter

下面是我用来上传不同文件的脚本。我发现的所有解决方案只涉及多个图片上传。我完全难以解决这个问题。有人可以告诉我,我应该做的是以相同的形式上传不同的文件吗?感谢

function do_upload()
{
    $config['upload_path'] = './uploads/nav';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '2000';

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

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

        return $error;
    }   
    else
    {

        $soundfig['upload_path'] = './uploads/nav';
        $soundfig['allowed_types'] = 'mp3|wav';

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

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

            return $error;
        }   
        else
        {

            $data = $this->upload->data('userfile');
            $sound = $this->upload->data('soundfile');
            $full_path = 'uploads/nav/' . $data['file_name'];
            $sound_path = 'uploads/nav/' . $sound['file_name'];

            if($this->input->post('active') == '1'){
                $active = '1';
            }else{
                $active = '0';
            }

            $spam = array(
                'image_url' => $full_path,
                'sound' => $sound_path,
                'active' => $active,
                'url' => $this->input->post('url')
            );

            $id = $this->input->post('id');

            $this->db->where('id', $id);
            $this->db->update('NavItemData', $spam);

            return true;
        }       
    }
}

这是我的表格:

<?php echo form_open_multipart('upload/do_upload');?>
<?php if(isset($buttons)) : foreach($buttons as $row) : ?>
<h2><?php echo $row->name; ?></h2>
<input type="file" name="userfile" size="20" /><br />
<input type="file" name="soundfile" size="20" />
<input type="hidden" name="oldfile" value="<?php echo $row->image_url; ?>" />
<input type="hidden" name="id" value="<?php echo $row->id; ?>" />

<br /><br />
<label>Url: </label><input type="text" name="url" value="<?php echo $row->url; ?>" /><br />
<input type="checkbox" name="active" value="1" <?php if($row->active == '1') { echo 'checked'; } ?> /><br /><br />
<input type="submit" value="submit" />

</form>

<?php endforeach; ?>
<?php endif; ?>

2 个答案:

答案 0 :(得分:0)

如果您要上传所有类型的文件或使用该数组指定您要上传的文件类型,请从代码中删除$config['allowed_types']行,例如:

$config['allowed_types'] = 'gif|jpg|png|bmp|etc';

答案 1 :(得分:0)

此代码可以帮助您: -

        <select name="file_provider2">
            <option value="singa">Singapore</option>
        </select> 
          <input type="file" name="singafile" id="singafile">

        <select name="file_provider1">
            <option value="usa">USA</option>
        </select> 
          <input type="file" name="usafile" id="usafile">

控制器: -

if($this->input->post("file_provider1")=="usa"){
                if ($_FILES['usafile']['size'] > 0){
                 $file = $_FILES['usafile']['tmp_name'];  
               //all code goes here for first file
   }
}
if($this->input->post("file_provider2")=="singa"){
                if ($_FILES['usafile']['size'] > 0){
                $file = $_FILES['singafile']['tmp_name']; 
               //all code goes here for 2nd file
   }
}