33

我一直在寻找和挣扎 3 天来使这个工作,但我就是做不到。我想要做的是使用多文件输入表单,然后上传它们。我不能只使用固定数量的文件来上传。我在 StackOverflow 上尝试了许多解决方案,但我无法找到一个可行的解决方案。

这是我的上传控制器

<?php

class Upload extends CI_Controller {

function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'url','html'));
}

function index()
{    
    $this->load->view('pages/uploadform', array('error' => ' ' ));
}

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


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

 foreach($_FILES['userfile'] as $key => $value)
    {

        if( ! empty($key['name']))
        {

            $this->upload->initialize($config);

            if ( ! $this->upload->do_upload($key))
            {
                $error['error'] = $this->upload->display_errors();

                $this->load->view('pages/uploadform', $error);
            }    
            else
            {
                $data[$key] = array('upload_data' => $this->upload->data());

                $this->load->view('pages/uploadsuccess', $data[$key]);


            }
         }

    }    
  }    
 }
 ?> 

我的上传表格是这个。

 <html>
 <head>
    <title>Upload Form</title>
</head>
<body>

<?php echo $error;?>

<?php echo form_open_multipart('upload/do_upload');?>

<input type="file" multiple name="userfile[]" size="20" />
<br /><br />


<input type="submit" value="upload" />

</form>

</body>
</html> 

我只是一直有这个错误:

您没有选择要上传的文件。

这是示例的数组:

Array ( [userfile] => Array ( [name] => Array ( [0] => youtube.png [1] => zergling.jpg ) [type] => Array ( [0] => image/png [1 ] => image/jpeg ) [tmp_name] => 数组 ( [0] => E:\wamp\tmp\php7AC2.tmp [1] => E:\wamp\tmp\php7AC3.tmp ) [error] =>数组 ( [0] => 0 [1] => 0 ) [大小] => 数组 ( [0] => 35266 [1] => 186448 ) ) )

如果我选择 2 个文件,我会连续 5 次这样。我也使用标准的上传库。

4

16 回答 16

100

在你的帮助下,我终于成功了!

这是我的代码:

 function do_upload()
{       
    $this->load->library('upload');

    $files = $_FILES;
    $cpt = count($_FILES['userfile']['name']);
    for($i=0; $i<$cpt; $i++)
    {           
        $_FILES['userfile']['name']= $files['userfile']['name'][$i];
        $_FILES['userfile']['type']= $files['userfile']['type'][$i];
        $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
        $_FILES['userfile']['error']= $files['userfile']['error'][$i];
        $_FILES['userfile']['size']= $files['userfile']['size'][$i];    

        $this->upload->initialize($this->set_upload_options());
        $this->upload->do_upload();
    }
}

private function set_upload_options()
{   
    //upload an image options
    $config = array();
    $config['upload_path'] = './Images/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = '0';
    $config['overwrite']     = FALSE;

    return $config;
}

谢谢你们!

于 2012-07-18T10:13:23.417 回答
4

您应该使用此库在 CI https://github.com/stvnthomas/CodeIgniter-Multi-Upload中进行多次上传

安装 只需将 MY_Upload.php 文件复制到您的应用程序库目录。

使用:控制器中的函数 test_up

public function test_up(){
if($this->input->post('submit')){
    $path = './public/test_upload/';
    $this->load->library('upload');
    $this->upload->initialize(array(
        "upload_path"=>$path,
        "allowed_types"=>"*"
    ));
    if($this->upload->do_multi_upload("myfile")){
        echo '<pre>';
        print_r($this->upload->get_multi_upload_data());
        echo '</pre>';
    }
}else{
    $this->load->view('test/upload_view');
}

}

应用程序/视图/测试文件夹中的upload_view.php

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="myfile[]" id="myfile" multiple>
<input type="submit" name="submit" id="submit" value="submit"/>
于 2014-03-26T08:52:00.273 回答
3

试试这个代码。

它对我来说很好用

每次库都必须初始化

    function do_upload()
    {
        foreach ($_FILES as $index => $value)
        {
            if ($value['name'] != '')
            {
                $this->load->library('upload');
                $this->upload->initialize($this->set_upload_options());

                //upload the image
                if ( ! $this->upload->do_upload($index))
                {
                    $error['upload_error'] = $this->upload->display_errors("<span class='error'>", "</span>");
                    
                    //load the view and the layout
                    $this->load->view('pages/uploadform', $error);

                    return FALSE;
                }
                else
                {
                    
                     $data[$key] = array('upload_data' => $this->upload->data());

                     $this->load->view('pages/uploadsuccess', $data[$key]);

       
                }
            }
        }

    }

    private function set_upload_options()
    {   
        //upload an image options
        $config = array();
        $config['upload_path'] = 'your upload path';
        $config['allowed_types'] = 'gif|jpg|png';
        
        return $config;
    }

进一步编辑

我找到了您必须使用唯一输入框上传文件的方法

CodeIgniter 不支持多个文件。在 foreach 中使用 do_upload() 与在外部使用它没有什么不同。

您将需要在没有 CodeIgniter 帮助的情况下处理它。这是一个示例https://github.com/woxxy/FoOlSlide/blob/master/application/controllers/admin/series.php#L331-370

https://stackoverflow.com/a/9846065/1171049

这就是你在评论中说的:)

于 2012-07-17T21:45:33.450 回答
1

这里还有一段代码:

参考:https ://github.com/stvnthomas/CodeIgniter-Multi-Upload

于 2014-01-10T07:19:04.757 回答
1

正如 Carlos Rincones 所建议的那样;不要害怕与超全球玩家一起玩。

$files = $_FILES;

for($i=0; $i<count($files['userfile']['name']); $i++)
{
    $_FILES = array();
    foreach( $files['userfile'] as $k=>$v )
    {
        $_FILES['userfile'][$k] = $v[$i];                
    }

    $this->upload->do_upload('userfile')
}
于 2016-02-05T13:58:26.867 回答
1

所有已发布的文件都将包含在 $_FILES 变量中,为了使用 codeigniter 上传库,我们需要提供我们用于上传的 field_name(默认情况下它将是 'userfile'),所以我们获取所有发布的文件并创建另一个文件数组为每个文件创建我们自己的名称,并将此名称赋予 codeigniter 库 do_upload 函数。

if(!empty($_FILES)){
    $j = 1;                 
    foreach($_FILES as $filekey=>$fileattachments){
        foreach($fileattachments as $key=>$val){
            if(is_array($val)){
                $i = 1;
                foreach($val as $v){
                    $field_name = "multiple_".$filekey."_".$i;
                    $_FILES[$field_name][$key] = $v;
                    $i++;   
                }
            }else{
                $field_name = "single_".$filekey."_".$j;
                $_FILES[$field_name] = $fileattachments;
                $j++;
                break;
            }
        }                       
        // Unset the useless one 
        unset($_FILES[$filekey]);
    }
    foreach($_FILES as $field_name => $file){
        if(isset($file['error']) && $file['error']==0){
            $config['upload_path'] = [upload_path];
            $config['allowed_types'] = [allowed_types];
            $config['max_size'] = 100;
            $config['max_width'] = 1024;
            $config['max_height'] = 768;
            $this->load->library('upload', $config);
            $this->upload->initialize($config);

            if ( ! $this->upload->do_upload($field_name)){
                $error = array('error' => $this->upload->display_errors());
                echo "Error Message : ". $error['error'];
            }else{
                $data = $this->upload->data();
                echo "Uploaded FileName : ".$data['file_name'];
                // Code for insert into database
            }
        }
    }
}
于 2016-07-05T12:13:05.607 回答
0
    public function imageupload() 
    {

      $count = count($_FILES['userfile']['size']);

  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = 'gif|jpg|png|bmp';
  $config['max_size']   = '0';
  $config['max_width']  = '0';
  $config['max_height']  = '0';

  $config['image_library'] = 'gd2';
  $config['create_thumb'] = TRUE;
  $config['maintain_ratio'] = FALSE;
  $config['width'] = 50;
  $config['height'] = 50;

  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];  

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

         if ($this->upload->do_upload('userfile'))
         {
           $data['userfile'][$i] = $this->upload->data();
       $full_path = $data['userfile']['full_path'];


           $config['source_image'] = $full_path;
           $config['new_image'] = './uploads/resiezedImage';

           $this->load->library('image_lib', $config);
           $this->image_lib->resize(); 
           $this->image_lib->clear();

         }
         else
         {
           $data['upload_errors'][$i] = $this->upload->display_errors();
         } 
     }
  }
}
于 2015-10-10T09:10:55.050 回答
0

我在
我的控制器调用的自定义库中使用了下面的代码,如下所示,

function __construct() {<br />
   &nbsp;&nbsp;&nbsp; parent::__construct();<br />
   &nbsp;&nbsp;&nbsp;   $this->load->library('CommonMethods');<br />
}<br />

$config = array();<br />
$config['upload_path'] = 'assets/upload/images/';<br />
$config['allowed_types'] = 'gif|jpg|png|jpeg';<br />
$config['max_width'] = 150;<br />
$config['max_height'] = 150;<br />
$config['encrypt_name'] = TRUE;<br />
$config['overwrite'] = FALSE;<br />

// upload multiplefiles<br />
$fileUploadResponse = $this->commonmethods->do_upload_multiple_files('profile_picture', $config);

/**
 * do_upload_multiple_files - Multiple Methods
 * @param type $fieldName
 * @param type $options
 * @return type
 */
public function do_upload_multiple_files($fieldName, $options) {

    $response = array();
    $files = $_FILES;
    $cpt = count($_FILES[$fieldName]['name']);
    for($i=0; $i<$cpt; $i++)
    {           
        $_FILES[$fieldName]['name']= $files[$fieldName]['name'][$i];
        $_FILES[$fieldName]['type']= $files[$fieldName]['type'][$i];
        $_FILES[$fieldName]['tmp_name']= $files[$fieldName]['tmp_name'][$i];
        $_FILES[$fieldName]['error']= $files[$fieldName]['error'][$i];
        $_FILES[$fieldName]['size']= $files[$fieldName]['size'][$i];    

        $this->CI->load->library('upload');
        $this->CI->upload->initialize($options);

        //upload the image
        if (!$this->CI->upload->do_upload($fieldName)) {
            $response['erros'][] = $this->CI->upload->display_errors();
        } else {
            $response['result'][] = $this->CI->upload->data();
        }
    }

    return $response;
}
于 2015-12-14T07:32:59.933 回答
0
<form method="post" action="<?php echo base_url('submit'); ?>" enctype="multipart/form-data">
    <input type="file" name="userfile[]" id="userfile"  multiple="" accept="image/*">
</form>

型号:文件上传

class FilesUpload extends CI_Model {

    public function setFiles()
    {
        $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'] = 'assets/product/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '10000000';
                $config['max_width'] = '51024';
                $config['max_height'] = '5768';

                $this->load->library('upload', $config);
                if (!$this->upload->do_upload()) {
                    $data_error = array('msg' => $this->upload->display_errors());
                    var_dump($data_error);
                } else {
                    $data = $this->upload->data();
                }
                $name_array[] = $data['file_name'];
            }

        $names = implode(',', $name_array);

        return $names;
    }
}

控制器提交

class Submit extends CI_Controller {
    function __construct()
        {
        parent::__construct();
        $this->load->helper(array('html', 'url'));
        }

        public function index()
        {
        $this->load->model('FilesUpload');

        $data = $this->FilesUpload->setFiles();

        echo '<pre>';
        print_r($data);

    }
}
于 2016-02-16T12:01:13.723 回答
0
        // Change $_FILES to new vars and loop them
        foreach($_FILES['files'] as $key=>$val)
        {
            $i = 1;
            foreach($val as $v)
            {
                $field_name = "file_".$i;
                $_FILES[$field_name][$key] = $v;
                $i++;   
            }
        }
        // Unset the useless one ;)
        unset($_FILES['files']);

        // Put each errors and upload data to an array
        $error = array();
        $success = array();

        // main action to upload each file
        foreach($_FILES as $field_name => $file)
        {
            if ( ! $this->upload->do_upload($field_name))
            {
                echo ' failed ';
            }else{
                echo ' success ';
            }
        }
于 2016-03-30T12:49:25.417 回答
0
function imageUpload(){
            if ($this->input->post('submitImg') && !empty($_FILES['files']['name'])) {
                $filesCount = count($_FILES['files']['name']);
                $userID = $this->session->userdata('userID');
                $this->load->library('upload');

                $config['upload_path'] = './userdp/';
                $config['allowed_types'] = 'jpg|png|jpeg';
                $config['max_size'] = '9184928';
                $config['max_width']  = '5000';
                $config['max_height']  = '5000';

                $files = $_FILES;
                $cpt = count($_FILES['files']['name']);

                for($i = 0 ; $i < $cpt ; $i++){
                    $_FILES['files']['name']= $files['files']['name'][$i];
                    $_FILES['files']['type']= $files['files']['type'][$i];
                    $_FILES['files']['tmp_name']= $files['files']['tmp_name'][$i];
                    $_FILES['files']['error']= $files['files']['error'][$i];
                    $_FILES['files']['size']= $files['files']['size'][$i];    

                    $imageName = 'image_'.$userID.'_'.rand().'.png';

                    $config['file_name'] = $imageName;

                    $this->upload->initialize($config);
                    if($this->upload->do_upload('files')){
                        $fileData = $this->upload->data(); //it return
                        $uploadData[$i]['picturePath'] = $fileData['file_name'];
                    }
                }

                if (!empty($uploadData)) {
                    $imgInsert = $this->insert_model->insertImg($uploadData);
                    $statusMsg = $imgInsert?'Files uploaded successfully.':'Some problem occurred, please try again.';
                    $this->session->set_flashdata('statusMsg',$statusMsg);
                    redirect('home/user_dash');
                }
            }
            else{
                redirect('home/user_dash');
            }
        }

于 2017-04-03T16:59:21.077 回答
0

codeigniter 中没有预定义的方法可以一次上传多个文件,但是您可以以数组的形式发送文件并一一上传

这里是参考:这是在codeigniter 3.0.1中上传多个文件的最佳选择,预览https://codeaskbuzz.com/how-to-upload-multiple-file-in-codeigniter-framework/

于 2018-02-23T11:35:18.233 回答
0

所以我改变的是我每次加载上传库

                $config = array();
                $config['upload_path'] = $filePath;
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size']      = '0';
                $config['overwrite']     = FALSE;

                $files = $_FILES;
                $count = count($_FILES['nameUpload']['name']);


                for($i=0; $i<$count; $i++)
                {
                    $this->load->library('upload', $config);

                    $_FILES['nameUpload']['name']= $files['nameUpload']['name'][$i];
                    $_FILES['nameUpload']['type']= $files['nameUpload']['type'][$i];
                    $_FILES['nameUpload']['tmp_name']= $files['nameUpload']['tmp_name'][$i];
                    $_FILES['nameUpload']['error']= $files['nameUpload']['error'][$i];
                    $_FILES['nameUpload']['size']= $files['nameUpload']['size'][$i];

                    $this->upload->do_upload('nameUpload');
                }

它对我有用。

于 2020-02-26T22:02:54.777 回答
0

对于 CodeIgniter 3

<form action="<?php echo base_url('index.php/TestingController/insertdata') ?>" method="POST"
      enctype="multipart/form-data">
    <div class="form-group">
        <label for="">title</label>
        <input type="text" name="title" id="title" class="form-control">
    </div>
    <div class="form-group">
        <label for="">File</label>
        <input type="file" name="files" id="files" class="form-control">
    </div>
    <input type="submit" value="Submit" class="btn btn-primary">
</form>


public function insertdatanew()
{
    $this->load->library('upload');
    $files = $_FILES;
    $cpt = count($_FILES['filesdua']['name']);

    for ($i = 0; $i < $cpt; $i++) {
        $_FILES['filesdua']['name'] = $files['filesdua']['name'][$i];
        $_FILES['filesdua']['type'] = $files['filesdua']['type'][$i];
        $_FILES['filesdua']['tmp_name'] = $files['filesdua']['tmp_name'][$i];
        $_FILES['filesdua']['error'] = $files['filesdua']['error'][$i];
        $_FILES['filesdua']['size'] = $files['filesdua']['size'][$i];

        // fungsi uploud
        $config['upload_path']          = './uploads/testing/';
        $config['allowed_types']        = '*';
        $config['max_size']             = 0;
        $config['max_width']            = 0;
        $config['max_height']           = 0;
        $this->load->library('upload', $config);
        $this->upload->initialize($config);

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

            // $this->load->view('welcome_message', $error);
        } else {

            // menambil nilai value yang di upload  
            $data = array('upload_data' => $this->upload->data());
            $nilai = $data['upload_data']; 
            $filename = $nilai['file_name'];
            var_dump($filename);

            // $this->load->view('upload_success', $data);
        }
    }
    // var_dump($cpt);
}
于 2020-10-04T16:14:11.097 回答
-1

我最近正在研究它。试试这个功能:

/**
 * @return array an array of your files uploaded.
 */
private function _upload_files($field='userfile'){
    $files = array();
    foreach( $_FILES[$field] as $key => $all )
        foreach( $all as $i => $val )
            $files[$i][$key] = $val;

    $files_uploaded = array();
    for ($i=0; $i < count($files); $i++) { 
        $_FILES[$field] = $files[$i];
        if ($this->upload->do_upload($field))
            $files_uploaded[$i] = $this->upload->data($files);
        else
            $files_uploaded[$i] = null;
    }
    return $files_uploaded;
}

在你的情况下:

<input type="file" multiple name="images[]" size="20" />

或者

<input type="file" name="images[]">
<input type="file" name="images[]">
<input type="file" name="images[]">

在控制器中:

public function do_upload(){
    $config['upload_path'] = './Images/';
    $config['allowed_types'] = 'gif|jpg|png';
    //...

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

    if ($_FILES['images']) {
        $images= $this->_upload_files('images');
        print_r($images);
    }
}

PHP手册的一些基本参考:PHP文件上传

于 2014-01-07T23:05:18.207 回答
-1

保存,然后将变量 $_FILES 重新定义为您需要的任何内容。也许不是最好的解决方案,但这对我有用。

function do_upload()
{

    $this->load->library('upload');
    $this->upload->initialize($this->set_upload_options());

    $quantFiles = count($_FILES['userfile']['name']);


    for($i = 0; $i < $quantFiles ; $i++)
    {
        $arquivo[$i] = array
                    (
                        'userfile' => array 
                                        (
                                            'name' => $_FILES['userfile']['name'][$i],
                                            'type' => $_FILES['userfile']['type'][$i],
                                            'tmp_name' => $_FILES['userfile']['tmp_name'][$i],
                                            'error' => $_FILES['userfile']['error'][$i],
                                            'size' => $_FILES['userfile']['size'][$i]
                                        )
                    );
    }


    for($i = 0; $i < $quantFiles ; $i++)
    {
        $_FILES = '';
        $_FILES = $arquivo[$i];



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


            return FALSE;
        }
        else
        {

            $data[$i] = array('upload_data' => $this->upload->data());

            var_dump($this->upload->data());
        }


    }





    if(isset($error))
        {
            $this->index($error);
        }
        else
        {
            $this->index($data);
        }

}

建立配置的单独功能..

private function set_upload_options()
{   
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'xml|pdf';
    $config['max_size'] = '10000';

    return $config;
}
于 2016-01-16T17:41:12.487 回答