如何在codeigniter中上传多个文件?

时间:2016-02-10 05:57:11

标签: php codeigniter

我想用多个insert_id上传每个文件但不会发生。我不知道为什么?我知道这对我来说不是一个难题,而是对我而言!

   public function do_upload($act_id)
   {                     
    $config['upload_path'] = "../public/assets/images/documents/";
    $config['allowed_types'] = 'jpg|jpeg|gif|png|zip|docx|pdf|txt';
    $config['max_size'] = '100000';
    //$config['max_width'] = '1024';
    //$config['max_height'] = '768';

    foreach($act_id as $id){

        foreach($_FILES['add_file']['name'] as $fileName){

    $ext = end(explode(".", $fileName));

    $config['file_name'] = $id.'.'.$ext;
        }       
    $this->load->library('upload', $config);
    $this->upload->initialize($config);

    $imageName = $config['upload_path'].$id.'.'.$ext;   
    unlink($imageName);

    if(!$this->upload->do_upload('add_file'))
    {
        $error = array('error'=>$this->upload->display_errors());
        $this->load->view('disciplinary_action_register/disciplinary_action_register_form',$error);
    }               
    else
    {
        $data = array('upload_data'=>$this->upload->data());                                            
        $pictureName = array(
            'FILE_DETAILS' => $id.'.'.$ext  
        );

        $this->db->update('lib_disciplinary_action_register', $pictureName, array('DISIPLINARY_ACTION_REGISTER_ID' => $id));
    }
    }
}

1 个答案:

答案 0 :(得分:1)

如果有帮助,请使用此

function do_upload($act_id)
    {       
        $file_ary       = $this->reArrayFiles($_FILES['add_file']);
        $allowed        = array('png', 'jpg', 'gif','jpeg','gif','zip','docx','pdf','txt');
        $gal            = "../public/assets/images/documents/";
        foreach($act_id as $id)
        {   
            foreach ($file_ary as $file)
            {
                $extension = pathinfo($file['name'], PATHINFO_EXTENSION);
                if(!in_array(strtolower($extension), $allowed))
                {
                    continue;// or collect error if you  want       
                }           
                $uniquename = unique();//generates a unique name to avoid override 
                $fully      = $uniquename.'.'.$extension;                       
                if(move_uploaded_file($file['tmp_name'], $gal.'/'.$fully))
                {
                     $pictureName = array('FILE_DETAILS' => $fully  );
                     $this->db->update('lib_disciplinary_action_register', $pictureName, array('DISIPLINARY_ACTION_REGISTER_ID' => $id));
                }                   
            }
        }
    }
    function reArrayFiles(&$file_post) 
    {
        $file_ary = array();
        $file_count = count($file_post['name']);
        $file_keys = array_keys($file_post);
        for ($i=0; $i<$file_count; $i++) 
        {
            foreach ($file_keys as $key) 
            {
                $file_ary[$i][$key] = $file_post[$key][$i];
            }
        }

        return $file_ary;
    }