如何在codeigniter中上传多个带有多个输入的文件

时间:2017-02-22 13:09:45

标签: php file

如何在codeigniter中上传多个带有多个输入的文件。以下是我的代码。我想添加许多文件,但文件输入不同。

  if(!empty($_FILES['countryfile']['name']))
       {
        $filesCount = count($_FILES['countryfile']['name']);

                for($i = 0; $i < $filesCount; $i++)
                    {   
                        $imgFile=$_FILES['countryfile']['name'][$i];
                        $tmp_dir=$_FILES['countryfile']['tmp_name'][$i];
                        $imgSize=$_FILES['countryfile']['size'][$i];
                        //$upload_dir='../uploads/dish_images';
                        $imgExt=strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
                        $valid_extensions = array('jpeg', 'jpg', 'png', 'gif','pdf');
                        $image=rand(1000,10000).".".$imgExt; 

                         $config['upload_path'] = '../admin/upload_doc';
                         //$config['upload_path'] = 'http://teq-staging.com/maswad-phase2/admin/uploads/dish_images/';
                         $config['allowed_types'] = 'jpg|jpeg|png|gif|pdf';
                         $config['file_name'] = $_FILES['countryfile']['name'];
                         $upload_dir=$config['upload_path'];
                         //$config['file_name']=$image;
                         $upload_dir=$config['upload_path'];    


                        $this->upload->initialize($config);
                         if($this->upload->do_upload('countryfile')){
                             $upload_data = $this->upload->data();


                    }
                }   

1 个答案:

答案 0 :(得分:0)

上传form.php

<?php echo form_open_multipart('upload');  ?>
<p>
    <?php echo form_label('File 1', 'userfile') ?>
    <?php echo form_upload('userfile') ?>
</p>
<p>
    <?php echo form_label('File 2', 'userfile1') ?>
    <?php echo form_upload('userfile1') ?>
</p>
<p><?php echo form_submit('submit', 'Upload them files!') ?></p>
<?php form_close() ?>

控制器

function index()
{
    // Has the form been posted?
    if (isset($_POST['submit']))
    {
        // Load the library - no config specified here
        $this->load->library('upload');

        // Check if there was a file uploaded - there are other ways to
        // check this such as checking the 'error' for the file - if error
        // is 0, you are good to code
        if (!empty($_FILES['userfile']['name']))
        {
            // Specify configuration for File 1
            $config['upload_path'] = 'uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';       

            // Initialize config for File 1
            $this->upload->initialize($config);

            // Upload file 1
            if ($this->upload->do_upload('userfile'))
            {
                $data = $this->upload->data();
            }
            else
            {
                echo $this->upload->display_errors();
            }

        }

        // Do we have a second file?
        if (!empty($_FILES['userfile1']['name']))
        {
            // Config for File 2 - can be completely different to file 1's config
            // or if you want to stick with config for file 1, do nothing!
            $config['upload_path'] = 'uploads/dir2/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';

            // Initialize the new config
            $this->upload->initialize($config);

            // Upload the second file
            if ($this->upload->do_upload('userfile1'))
            {
                $data = $this->upload->data();
            }
            else
            {
                echo $this->upload->display_errors();
            }

        }
    }
    else
    {
        $this->load->view("upload_form");
    }
}