如何在codeigniter中

时间:2018-06-03 08:39:53

标签: php codeigniter

我试图在数据库中一次上传两个图像,从具有两个不同文件输入字段的单个表单提交尝试。 我尝试但是当我尝试使用print_r显示结果时它不起作用它为两个输入字段提供了单个文件名。我怎样才能在codeigniter中完美地完成它。请帮助我上传我的代码。如果有任何错误,请告诉我。

HTML表单代码

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

我的Codeigniter控制代码

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 个答案:

答案 0 :(得分:3)

使用$this->upload->data('file_name'),您只能获得上次上传的文件名。你错过了第一个。要解决这个简单的问题,请将两个文件名存储在变量中,以便在两个文件成功上载后使用。

<强>之前:

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

添加

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

<强>替换

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

。通过

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

另一个问题:

在您当前的代码中,如果您的第一次上传成功,则第二次上传将失败。为了防止这种情况。

<强>替换

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

。通过

$upload1_ok = $this->upload->do_upload('img');
$upload2_ok = $this->upload->do_upload('img_2');

if($upload1_ok == false && $upload2_ok == false)