【问题标题】:How to save images inside a form with CodeIgniter如何使用 CodeIgniter 将图像保存在表单中
【发布时间】:2020-04-17 11:31:48
【问题描述】:

所以我也有一个带有一些文本字段和三个文件字段的表单。在提交时,我想将新用户插入到我的数据库中,并且我想将三个具有不同名称的单独文件(图像)保存到我的服务器。我阅读了 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');
}
}

查看

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

【问题讨论】:

    标签: php html codeigniter-3 image-uploading form-submit


    【解决方案1】:

    首先将&lt;form...更改为:

    <form role="form" method="post" enctype="multipart/form-data" id="new-user-form" class="needs-validation" action="<?= base_url(); ?>test/newUser" novalidate>
    

    然后检查:Multiple image upload with CodeIgniter

    【讨论】:

    • 这对我帮助不大。我到处都能找到这个多文件上传,但对我来说,这并不好。此外,它只是文件上传。我想以一种我还想获取其他输入字段的形式实现它,然后将其插入我的数据库并保存图像。我必须这样做,因为这三个图像有它们的作用。不能少也不能多,而且用户还必须知道哪个角色将担任哪个角色,所以我不能用多个输入字段来做到这一点。
    • 如果您想上传 3 张单独的图像而不是需要多个输入字段。您可以将它们放在前端并以不同的方式命名它们,以便您和用户知道哪个是哪个。当前的问题是,codeigniter 仅支持每个表单提交 1 个图像上传,这就是为什么您需要一个可以处理多个上传或编写自己的实现的单独库的原因。我给您的链接显示了该部分,您可以简单地将其添加到您自己的代码中。还有一个选择,那就是使用 ajax 上传,但使用 php 会更容易。
    猜你喜欢
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    相关资源
    最近更新 更多