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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?