PHP multipart/form Data

I'm trying to upload a file , but file name is a variable,so to do that I have get the name of input type(i can do it by getting all data on post and run a loop and get the keyname)

then I'm getting all post data , but then it returns me a empty array,when I remove multipart/form data from the form it will return the array with the value.then I can get the file type name, but it doesn't upload the file because I remove the multipart from the form. How can I do this? if read the code u will understand. Please help me to do this. This is my view page

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

    }
}

And this my View

<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 answers

  • answered 2018-01-14 11:36 Quentin

    You need multipart/form-data for the rules to be uploaded. Put it back.

    Then loop over $_FILES to get all the files that were uploaded.

  • answered 2018-01-14 11:36 Italo Hernández

    multipart/form-data MUST be there for file uploads

    You can try something like this:

    $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
            }
        }
    }