使用codeigniter上传多个文件的正确方法

时间:2014-08-22 16:48:17

标签: codeigniter

我正在尝试一次上传多个图片,但此代码包含错误,错误消息显示未定义索引:photo1A。所以帮我解决一下。

function add_student_database(){

  //echo '<pre>';print_r($_POST);print_r($_FILES);exit;
  $this->load->library('upload');


  if($this->input->post('stu_class')=='first' && $this->input->post('section')=='A')
  {
     $data = $this->input->post('student1A');
     $img_data = $_FILES['photo1A'];
    // echo '<pre>';print_r($_FILES);exit;
     $count = count($_FILES['photo1A']);
        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'jpg|png|gif';
        $config['max_size'] = '100000';
        $config['max_width'] = '2000';
        $config['max_height'] = '2000';
     $this->load->library('upload', $config);
     for($i=0;$i<$count;$i++)
     {

        $_FILES['photo1A']['name'] = $img_data['photo1A']['name'][$i];
        $_FILES['photo1A']['type'] = $img_data['photo1A']['type'][$i];
        $_FILES['photo1A']['tmp_name'] = $img_data['photo1A']['tmp_name'][$i];
        $_FILES['photo1A']['error'] = $img_data['photo1A']['error'][$i];
        $_FILES['photo1A']['size'] = $img_data['photo1A']['size'][$i];

        $this->upload->initialize($config);
        $this->upload->do_upload('photo1A');
     }

     $section = "A";
     $stu_class = "class1";

  }
  elseif($this->input->post('stu_class')=='first' && $this->input->post('section')=='B')
  {
     $data = $this->input->post('student1B');
     $section = "B";
     $stu_class = "class1";

  }
  elseif($this->input->post('stu_class')=='second' && $this->input->post('section')=='A')
  {
     $data = $this->input->post('student2A');
     $section = "A";
     $stu_class = "class2";

  }
  elseif($this->input->post('stu_class')=='second' && $this->input->post('section')=='B')
  {
     $data = $this->input->post('student2B');
     $section = "B";
     $stu_class = "class2";

  }

  $this->marksheet_data->get_data($data, $section, $stu_class);
}

1 个答案:

答案 0 :(得分:0)

    $message="";
    $error=0;

$data1 = $this->input->post('data1');
$data2 = $this->input->post('data2');
$data3 = $this->input->post('data3');
$data.. = $this->input->post('data..');
$datan = $this->input->post('datan');
    if($error==0)
    {



        $newdata = array(
            'colName1_of_table' => $data1,
    'colName2_of_table' => $data1,
    'colName..._of_table'=>$data..,
    'colNamen_of_table'=>$datan
        );

        $insert = $this->db->insert('tblName', $newdata);

        $lastid=$this->db->insert_id();

        if($insert)
        {

        //.............image upload.......................................//
            $config['upload_path'] = PHYSICAL_PATH.'design/front/';
            //$config['allowed_types'] = 'gif|png|jpeg|jpg'; 
    $config['allowed_types'] = '*'; //allow all type of file with any extension
            $config['max_size']  = 1024 * 8;
            $config['encrypt_name'] = TRUE;
            $this->load->library('upload', $config);
            $status = "";
            $msg = "";
            $userpicture='';
            if(!empty($_FILES["inputTAGnameOFfirstImage"]["name"]))
            {
            $file_element_name = 'inputTAGnameOFfirstImage';

            if (!$this->upload->do_upload($file_element_name))
            {
                $status = 'error';
                $msg = $this->upload->display_errors('', '');
            }
            else
            {
               $data = $this->upload->data();

               if($data)
               {

                $config = array(
                 'source_image' => $data['full_path'],
                 'new_image' => $config['upload_path'] . '/thumbnail/',
                 'maintain_ratio' => true,
                 'width' => 100,
                 'height' => 100
                 );

              $this->load->library('image_lib', $config);
              $this->image_lib->resize();
              $this->image_lib->clear(); /*this is important*/
              $userpicture = $data['file_name'];

               }

            }

            $image_data=array(
            'colNameofImage_to_store' => $userpicture
            );
            $this->db->where('id', $lastid);
            $insert_image = $this->db->update('tblName',$image_data);

            }

    //for Next image

    $config1['upload_path'] = PHYSICAL_PATH.'design/back/';
            //$config1['allowed_types'] = 'gif|png|jpeg|jpg';
    $config1['allowed_types'] = '*';
            $config1['max_size']  = 1024 * 8;
            $config1['encrypt_name'] = TRUE;

    $this->load->library('upload', $config1);
    $this->upload->initialize($config1);
            $status = "";
            $msg = "";
            $userpicture1='';

            if(!empty($_FILES["inputTAGnameOFsecondImage"]["name"]))
            {

            $file_element_name = 'inputTAGnameOFsecondImage';

            if (!$this->upload->do_upload($file_element_name))
            {
                $status = 'error';
               echo $msg = $this->upload->display_errors('', '');
            }
            else
            {

               $data1 = $this->upload->data();

               if($data1)
               {

    $config2 = array(
                 'source_image' => $data1['full_path'],
                 'new_image' => $config1['upload_path'] . '/thumbnail/',
                 'maintain_ratio' => true,
                 'width' => 100,
                 'height' => 100
                 );



    // Initialize

      $this->load->library('image_lib', $config2, 'image_lib_thumb');
      //$this->image_lib->resize();
      $this->image_lib_thumb->resize();
     $this->image_lib_thumb->clear(); /*this is important*/

              $userpicture1 = $data1['file_name'];
               }

            }

            $new_image_data=array(
            'second Image _colName_inTbl' => $userpicture1
            );
            $this->db->where('id', $lastid);
            $insert_image = $this->db->update('tblName',$new_image_data);

            }

        }



        return $insert;
    }

希望这对你有所帮助。欢迎提问。按照您的需要循环,就像您尝试的那样。