简体   繁体   English

文件上传在Codeigniter中无法正常工作

[英]File uploading is not working properly in codeigniter

I am using code-igniter to upload files with two input boxes . 我正在使用代码点火器上传带有两个输入框的文件。 But my code not uploading both the files im getting same file two times. 但是我的代码没有两次上传两个文件,而两次却获得了相同的文件。

please take a look my code -: 请看看我的代码-:

**//html**

<?php echo form_open_multipart('vipul/do_upload');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='file' name='userfile1' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?> 

in controller 在控制器中

public function do_upload(){

    foreach ($_FILES as $keys=>$values){

        if($keys == 'userfile1'){
            $config = array(
                        'upload_path' => "./uploads/",
                        'allowed_types' => "gif|jpg|png|jpeg|pdf",
                        'overwrite' => false,
                        'max_size' => "20480000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
                        'max_height' => "768000",
                        'max_width' => "1024000"
                        );
                $this->upload->initialize($config);
                $this->load->library('upload', $config);
                if($this->upload->do_upload())
                { $data = array('upload_data' => $this->upload->data());}
                else{   $error = array('error' => $this->upload->display_errors()); }
        }

        if($keys == 'userfile'){
            $config = array(
                'upload_path' => "./uploads/",
                'allowed_types' => "gif|jpg|png|jpeg|pdf",
                'overwrite' => false,
                'max_size' => "20480000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
                'max_height' => "768000",
                'max_width' => "1024000"
                );
            $this->load->library('upload', $config);
            if($this->upload->do_upload())
            {   $data = array('upload_data' => $this->upload->data());  }
            else{   $error = array('error' => $this->upload->display_errors()); }
        }

    }

Please change your PHP code and try this out 请更改您的PHP代码,然后尝试一下

$this->upload->do_upload()

to

$this->upload->do_upload($field_name)

where $field_name is your input name attribute. 其中$field_name是您的输入名称属性。

please check CodeIgniter's File Uploading Class 请检查CodeIgniter的文件上传类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM