PHP multipart / form数据

时间:2018-01-14 11:36:11

标签: php codeigniter

我试图上传文件,但文件名是一个变量,所以为了做到这一点,我得到了输入类型的名称(我可以通过获取帖子上的所有数据并运行循环获取关键名)

然后我获取所有发布数据,但随后它返回一个空数组,当我从表单中删除multipart / form数据时,它将返回带有value的数组。然后我可以获取文件类型名称,但它没有上传文件,因为我从表单中删除了多部分。 我怎样才能做到这一点?如果读了你会理解的代码。请帮我这样做。 这是我的观看页面

public function addTest($app_id)
{
    $data=$this->input->post();
    $id=$this->session->userdata['loggedHospitalData']['hp_id'];


    while ($test = current($data)) {

        $dataArr=array(
                        "hospital_id" => $id,

                   );

        $test_id=key($data);

        $wherArr = array("test_id"=>$test_id);
        //$result = $this->Hospital_model->updateTestData($dataArr,$wherArr);


        $report=$this->input->post($test_id);




            $config['upload_path']          = './uploads/';
            $config['allowed_types']        = '*';
            $config['max_size']             = 5000000000000;
            $config['max_width']            = 10240;
            $config['max_height']           = 10240;

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


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


            }
            else
            {

                    $data1 = array('upload_data' => $this->upload->data());


            }






        /*$dataRepArr=array(
                        "r_test_id " => $test_id,
                        "file" => $photo1,
                   );*/

        next($data);



//header('Location: http://localhost/docto/index.php/Hospital/patientView/'.$app_id);

    }
}

这是我的观点

<form action="<?php echo base_url(); ?>index.php/Hospital/addTest/<?php echo $appoinmentData->app_id; ?>" role="form"  method='post' onmouseover="return form_validate()" name="vform">
                  <div class="">
                    <ul class="to_do">

                      <?php

                      foreach ($testData as $test) {

                     echo '

                      <li>
                        <p>

                           '.$test->test_name.' </p>

                          </br>
                          <input type="file" class="btn btn-primary" name="'.$test->test_id.'">

                      </li>

                       ';

                       }


                      ?>
                    </ul>
                  </div>


                <input type="submit" class="btn btn-success btn-lg btn-block" value="Update" name="">
                </form>

2 个答案:

答案 0 :(得分:0)

您需要multipart/form-data才能上传规则。把它放回去。

然后循环$_FILES以获取上传的所有文件。

答案 1 :(得分:0)

multipart / form-data 必须用于文件上传

您可以尝试这样的事情:

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

$config['upload_path']      =   './assets/uploads/'
$config['allowed_types']    =   'jpg|png|jpeg|pdf';
$config['max_size']     =   '0';            
$config['overwrite']        =   TRUE;
$config['encrypt_name']     =   TRUE;

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

foreach($_FILES as $k => $v)
{   
    if ($_FILES[$k]['name'] != '')
    {                                   
        //This do the trick,  do_upload need the name of the input
        //And in this case, name is the KEY of an array.
        if($this->upload->do_upload($k))
        {
            //Upload Successful
        }
    }
}