上传图片和数据动态创建codeigniter

时间:2016-03-26 16:25:08

标签: php jquery html codeigniter-3

我需要上传图片和数据动态创建html和jQuery我使用codeigneter框架并将所有数据发送到mysql。

这是代码控制器

public function Addtest() {
 $this->load->library('upload');
    $u=  $this->session->userdata('id_user');
   $files = $_FILES;
    $cpt = count($_FILES['sliderimg']['name']);
    $inputAll=  $this->input->post();

 $this->load->model('AddData');

 for($i=0; $i < count($inputAll['artilce']); $i++) {
         $_FILES['sliderimg']['name']= $files['sliderimg']['name'][$i];
    $_FILES['sliderimg']['type']= $files['sliderimg']['type'][$i];
  //  $_FILES['sliderimg']['tmp_name']= $files['sliderimg']['tmp_name'][$i];

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

    var_dump($_FILES);
    $data=  array(

        'id_user'=>$u,
        'description2'=>$inputAll['description2'][$i],
        'description1'=>$inputAll['description1'][$i],
        'art'=>$inputAll['artilce'][$i],

       $_FILES['sliderimg']['name']= $files['sliderimg']['name'][$i],


         $image_name =>$_FILES['sliderimg']['name'][$i],
    );

if( $this->AddData->Add_data($data)) {
       echo 'false';
} else {
       echo 'true';
}  

这是模型代码

 public function Add_data($artt) {
    extract($artt);

    $data = array(
        'Art'=>$art,
        'des1'=>$description1,
        'desc2'=>$description2,
        'picture'=>$image_name,
        'id_foreign'=>$id_user,
    );

    $this->db->insert('table',$data);
}

这是我尝试上传2张图片和数据时的错误输出

  

遇到PHP错误

     

严重性:注意

     

消息:未定义的变量:image_name

这是set_upload_options()函数

private function set_upload_options()
{   
    $config = array();
    $config['upload_path']   = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size'] = '2048000';
    $config['max_width']  = '20000';
    $config['max_height']  = '17680';
    $config['overwrite']     = FALSE;

    return $config;
}

do_uplaod

public function  do_upload() {
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size'] = '2048000';
    $config['max_width']  = '2024';
    $config['max_height']  = '1068';

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

    if ( ! $this->upload->do_upload())
    {
        return "false";
    }
    else
    {
        return "true";
    }
}

0 个答案:

没有答案