Php 如何使用CodeIgniter在表单中保存图像

Php 如何使用CodeIgniter在表单中保存图像,php,html,codeigniter-3,image-uploading,form-submit,Php,Html,Codeigniter 3,Image Uploading,Form Submit,所以我有一个表单,有一些文本字段和三个文件字段。在提交时,我想将新用户插入我的数据库,并将三个不同名称的单独文件(图像)保存到我的服务器。我读过CodeIgniter的文件上传类,但我无法在代码中实现它 我就是这么想的 控制器 <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Test extends CI_Controller { public function __construct

所以我有一个表单,有一些文本字段和三个文件字段。在提交时,我想将新用户插入我的数据库,并将三个不同名称的单独文件(图像)保存到我的服务器。我读过CodeIgniter的文件上传类,但我无法在代码中实现它

我就是这么想的

控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Test extends CI_Controller {
public function __construct() {
    parent::__construct();
    if (!is_logged_in()) {
        redirect('login');
    }
}

public function index()
{
    $this->load->view('templates/header');
    $this->load->view('pages/test_view');
    $this->load->view('templates/footer');
}

public function newUser(){
    $this->load->model('TestModel');

    $new_user = array (
        'FirstName' => $this->input->post('inputFirstName'),
        'LastName' => $this->input->post('inputLastName')
    );

    $insert_id = $this->TestModel->insertNewUser($new_user);

    $config['upload_path'] = base_url().'img/users/';
    $config['allowed_types'] = 'jpeg|jpg|png';
    $config['overwrite'] = TRUE;
    $config['file_name'] = $insert_id."_index";

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

    if ($this->upload->do_upload('index'))
    {
        $data = array('upload_data' => $this->upload->data());
    } else {
        $error = array('error' => $this->upload->display_errors());
    }

    $config['file_name'] = $insert_id."_picture1";
    $this->load->library('upload', $config);

    if ($this->upload->do_upload('picture1'))
    {
        $data = array('upload_data' => $this->upload->data());
    } else {
        $error = array('error' => $this->upload->display_errors());
    }

    $config['file_name'] = $insert_id."_picture2";
    $this->load->library('upload', $config);

    if ($this->upload->do_upload('picture2'))
    {
        $data = array('upload_data' => $this->upload->data());
    } else {
        $error = array('error' => $this->upload->display_errors());
    }

    //redirect('test');
}
}

第一次更改
对我帮助不大。我到处都能找到这种多文件上传,但对我来说,这并不好。此外,它只是文件上传。我想以一种形式实现它,在这种形式中,我还想获取一些其他输入字段,然后将其插入到我的数据库中,并保存图像。我必须这样做,因为这三个图像有各自的角色。不能少或多,用户还必须知道哪个角色将担任哪个角色,因此我不能使用多个输入字段。如果您要上载3个单独的图像,则需要多个输入字段。您可以在前端将它们隔开并以不同的方式命名,以便您和用户知道哪个是哪个。当前的问题是codeigniter只支持每个表单提交1个图像上载,这就是为什么您需要一个单独的库来处理多个上载或编写自己的实现。我提供给您的链接显示了该部分,您可以简单地将其添加到自己的代码中。还有一个选项是ajax上传,但使用php会更容易。
<div class="container">
    <div class="row justify-content-center">
        <div class="col text-center">
            <!-- Button trigger modal -->
            <button type="button" class="btn btn-primary margin-t newButton" data-toggle="modal" data-target="#newUserModal">
                Add user
            </button>
        </div>
    </div>
</div>

<!-- New User Modal -->
<div class="modal fade" id="newUserModal" tabindex="-1" role="dialog" aria-labelledby="newUserModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="newUserModalLabel">New user</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form role="form" method="post" id="new-user-form" class="needs-validation" action="<?= base_url(); ?>test/newUser" novalidate>
                    <div class="form-row">
                        <div class="col-md-6 mb-3">
                            <label for="inputFirstName">First name</label>
                            <input type="text" class="form-control" name="inputFirstName" id="inputFirstName" placeholder="" required>
                            <div class="invalid-feedback">
                                Invalid input
                            </div>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="inputLastName">Last name</label>
                            <input type="text" class="form-control" name="inputLastName" id="inputLastName" placeholder="" required>
                            <div class="invalid-feedback">
                                Invalid input
                            </div>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="col-md-4 mb-3">
                            <div class="avatar-upload">
                                <div class="avatar-edit">
                                    <input type='file' name="index" id="indexImageUpload" accept=".png, .jpg, .jpeg" />
                                    <label class="text-center" for="indexImageUpload"></label>
                                </div>
                                <div class="avatar-preview">
                                    <div id="indexImage" style="background-image: url(https://ryanacademy.ie/wp-content/uploads/2017/04/user-placeholder.png)">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4 mb-3">
                            <div class="avatar-upload">
                                <div class="avatar-edit">
                                    <input type='file' name="picture1" id="picture1Upload" accept=".png, .jpg, .jpeg" />
                                    <label class="text-center" for="picture1Upload"></label>
                                </div>
                                <div class="avatar-preview">
                                    <div id="picture1" style="background-image: url(https://ryanacademy.ie/wp-content/uploads/2017/04/user-placeholder.png)">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4 mb-3">
                            <div class="avatar-upload">
                                <div class="avatar-edit">
                                    <input type='file' name="picture2" id="picture2Upload" accept=".png, .jpg, .jpeg" />
                                    <label class="text-center" for="picture2Upload"></label>
                                </div>
                                <div class="avatar-preview">
                                    <div id="picture2" style="background-image: url(https://ryanacademy.ie/wp-content/uploads/2017/04/user-placeholder.png)">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary closeButton" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary" form="new-user-form">Save</button>
            </div>
        </div>
    </div>
</div>
<form role="form" method="post" enctype="multipart/form-data" id="new-user-form" class="needs-validation" action="<?= base_url(); ?>test/newUser" novalidate>