上传文件不会工作codeigniter

时间:2015-03-18 16:13:03

标签: codeigniter file-upload

我在墙上撞了大约3个小时。我试图在互联网上搜索为什么要上传的文件不会上传到文件夹中,你能帮帮我吗?

文件上传权限设置为0777

这是我的观点

  <?php echo form_open_multipart('home_members/do_upload', array('class' => 'form-horizontal','id' => 'MyForm')); ?>

     <div class="control-group">
        <label class="control-label" for="scan">Upload File:</label>
        <div class="controls">
            <input class="input-xlarge" name="userfile[]" id="userfile" type="file" multiple="" />
        </div>
    </div>     

    <div class="form-actions">
         <input type="submit" name="submit" value="Sell Card" class="btn btn-primary" />
    </div>                         
    <?php echo form_close() ?>

控制器:

public function do_upload() 
{
    $name_array = array();
    $count = count($_FILES['userfile']['size']);

    foreach($_FILES as $key=>$value)
        for($s=0; $s<=$count-1; $s++) {

            $_FILES['userfile']['name']=$value['name'][$s];
            $_FILES['userfile']['type']    = $value['type'][$s];
            $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
            $_FILES['userfile']['error']       = $value['error'][$s];
            $_FILES['userfile']['size']    = $value['size'][$s];  

            $config['upload_path'] = base_url() . 'uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';

            $this->load->library('upload', $config);
            $this->upload->do_upload();
            $data = $this->upload->data();
            $name_array[] = $data['file_name'];
        }
        $names= implode(',', $name_array);
        /* $this->load->database();
        $db_data = array('id'=> NULL,
        'name'=> $names);
        $this->db->insert('testtable',$db_data);
        */  print_r($names);

}   

1 个答案:

答案 0 :(得分:1)

如果使用codeigniter上传库,则无法将输入文件名设置为使用userfile[]的数组,应将其命名为userfile,如果要进行多个文件上传,则应将其命名为userfile1userfile2 ...记住它不应该是数组。

注意我对数组输入名称不是100%肯定 你可以像这样解决你的问题

<input class="input-xlarge" name="userfile1" id="userfile1" type="file"/>
<input class="input-xlarge" name="userfile2" id="userfile2" type="file"/>

现在你的上传功能应该是这样的

public function do_upload() 
{

    $CI = & get_instance();
    //if you are inside controller you can use $this
    $CI->load->library('upload');
    $config=array();
    $config['upload_path'] = FCPATH.'uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $config['overwrite'] = false;
    $config['remove_spaces'] = true;

    $name_array = array();
    foreach ($_FILES as $key => $value)
    {

            $CI->upload->initialize($config);
            if ($CI->upload->do_upload($key))
            {
                $data = $this->upload->data();
                $name_array[] = $data['file_name'];//I normally use $name_array[] = $data.Then check it with my expected input fields
            }

    }
    print_r($name_array);
}

希望它可以帮到你。