duanju8308 2018-06-03 08:39
浏览 57
已采纳

如何在codeigniter中的单页上添加两个不同输入的两个图像

I am trying to upload two images in database at once submit attempt from single form that have two different file input fields. I tried but its not working when i try to show result using print_r it gives a single file name for both input fields. How can i do it perfectly in codeigniter. please help for your reference i am uploading my code. you if any error please let me know.

HTML Form Code

<form id="form_edit" method="post" action="<?php echo base_url(" admin/aboutus/update/".$aboutus->id); ?>" enctype="multipart/form-data" accept-charset="utf-8">
    <div class="form-group row">
        <div class="col-md-4 col-xs-4">
            <input type="text" name="imgtitle" title="About Us Image Title" class="form-control  input-sm" value="<?php echo set_value('imgtitle', $aboutus->imgtitle); ?>" placeholder="Image Title" required="required">
            <?php echo form_textarea(['rows'=>'15', 'name'=>'imgdetail','title'=>'About Us Image Description','class'=>'form-control mptop input-sm','required'=>'required','value'=> set_value('detail', $aboutus->imgdetail)]); ?>
            <input type="file" name="img" title="About Us Image" class="form-control input-sm">
        </div>
        <div class="col-md-4 col-xs-4">
            <input type="text" title="About Us Title" class="form-control input-sm" name="title" value="<?php echo set_value('title', $aboutus->title); ?>" placeholder="Type left side title of about us of maximum 15 characters" required="required">
            <?php echo form_textarea(['rows'=>'15', 'name'=>'detail','title'=>"About Us Detail",'class'=>'form-control mptop input-sm mptop','required'=>'required','value'=> set_value('detail', $aboutus->detail)]); ?>
        </div>
        <div class="col-md-4 col-xs-4">
            <input type="text" name="img_2title" title="About Us Image Title" class="form-control  input-sm" value="<?php echo set_value('imgtitle', $aboutus->img_2title); ?>" placeholder="Image Title" required="required">
            <?php echo form_textarea(['rows'=>'15', 'name'=>'img_2detail','title'=>'About Us Image Description','class'=>'form-control mptop input-sm','required'=>'required','value'=> set_value('detail', $aboutus->img_2detail)]); ?>
            <input type="file" name="img_2" title="About Us Image" class="form-control input-sm">
            <!-- id="detail" -->
        </div>
    </div>
    <div class="form-group row">
        <div class="col-md-12 col-xs-12 mptop rmzero rpZero">
            <div class="btn-group pull-right text-right">
                <a href="<?php echo base_url('admin/dashboard');?>" class="btn btn-warning btn-sm"><i class="fa fa-arrow-left"></i> Back</a>
                <!-- <a href="<?= base_url('fassets/images/aboutus/'.$aboutus->img); ?>" class="btn btn-info btn-sm" name="preview" id="save"><i class="fa fa-search"></i> Preview</a> -->
                <button type="submit" class="btn btn-success btn-sm" name="submit" id="save"><i class="fa fa-save"></i> Save</button>
            </div>
        </div>
    </div>
</form>

My Codeigniter Control Code

public function update($id)
{

    $res = array();     
    //form field validation rules
    $this->form_validation->set_rules('title', 'Title', 'required|max_length[15]');
    if (!$this->form_validation->run()) {
        echo json_encode(array('mes' => 'text-danger', 'msg' => validation_errors('')));
        exit;
    }   
    $this->form_validation->set_rules('detail', 'Detail', 'required|max_length[2000]'); 
    if (!$this->form_validation->run()) {
        echo json_encode(array('mes' => 'text-danger', 'msg' => validation_errors('')));
        exit;
    }   
    $this->form_validation->set_rules('imgtitle', 'Image Title', 'required|max_length[15]');
    if (!$this->form_validation->run()) {
        echo json_encode(array('mes' => 'text-danger', 'msg' => validation_errors('')));
        exit;
    }   
    $this->form_validation->set_rules('imgdetail', 'Image Description', 'required');
    if (!$this->form_validation->run()) {
        echo json_encode(array('mes' => 'text-danger', 'msg' => validation_errors('')));
        exit;
    }   
    $this->form_validation->set_rules('img_2title', 'Second Image Title', 'required|max_length[15]');
    if (!$this->form_validation->run()) {
        echo json_encode(array('mes' => 'text-danger', 'msg' => validation_errors('')));
        exit;
    }   
    $this->form_validation->set_rules('img_2detail', 'Second Image Description', 'required');
    if (!$this->form_validation->run()) {
        echo json_encode(array('mes' => 'text-danger', 'msg' => validation_errors('')));
        exit;
    }   

    $config['upload_path'] = 'fassets/images/aboutus';
    $config['allowed_types'] = 'jpg|jpeg|png|gif';
    //$config['file_name'] = $_FILES['img']['name'];
    $config['overwrite'] = TRUE;
    //Load upload library and initialize configuration
    $this->load->library('upload', $config);        
        // echo $image_path; exit;      
    if((!$this->upload->do_upload('img')) && (!$this->upload->do_upload('img_2')))
    {
        $userData = array(
            'title' => $this->input->post('title'),
            'detail' => $this->input->post('detail'),   
            'imgtitle' => $this->input->post('imgtitle'),
            'imgdetail' => $this->input->post('imgdetail'), 
            'img_2title' => $this->input->post('img_2title'),
            'img_2detail' => $this->input->post('img_2detail')          

        );
        //Pass user data to model
        $insertUserData = $this->AboutusModel->update($userData, $id);
    }
    else
    {

        $userData = array(
            'title' => $this->input->post('title'),
            'detail' => $this->input->post('detail'),   
            'imgtitle' => $this->input->post('imgtitle'),
            'imgdetail' => $this->input->post('imgdetail'),
            'img_2title' => $this->input->post('img_2title'),
            'img_2detail' => $this->input->post('img_2detail'),             
            'img' =>  $this->upload->data('file_name'),
            'img_2' =>  $this->upload->data('file_name')
        );
        echo "<pre>";
        print_r($userData);
        exit;
        //Pass user data to model
        $insertUserData = $this->AboutusModel->update($userData, $id);
    }
        //Storing insertion status message.
    if($insertUserData){
        $res = array(
            'mes' => 'text-success',
            'msg' => "Record has been saved successfully.",
        );
        echo json_encode($res);                     
    } else {
        $res = array(
            'mes' => 'text-danger',
            'msg' => "Oops! Something went wrong.",
        );
        echo json_encode($res);
    }           
}
  • 写回答

1条回答 默认 最新

  • dsk61780 2018-06-03 09:17
    关注

    With $this->upload->data('file_name'), you are getting only the last uploaded file name. You're missing the first one. To solve this simple issue, store both filenames in variables to use after both files have successfully been uploaded.

    Before:

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

    Add:

    $img1 = $_FILES['img']['name'];
    $img2 = $_FILES['img_2']['name'];
    

    Replace:

    'img' =>  $this->upload->data('file_name'),
    'img_2' =>  $this->upload->data('file_name')
    

    With:

    'img' =>  $img1,
    'img_2' =>  $img2
    

    Another Issue:

    In your current code, if your first upload succeeds the second will fail. To prevent that.

    Replace:

    if((!$this->upload->do_upload('img')) && (!$this->upload->do_upload('img_2')))
    

    With:

    $upload1_ok = $this->upload->do_upload('img');
    $upload2_ok = $this->upload->do_upload('img_2');
    
    if($upload1_ok == false && $upload2_ok == false)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?