简体   繁体   English

上传图像在 codeigniter 3 中不起作用

[英]uploading image not working in codeigniter 3

i'm new learning codeigniter 3, i have problem that i can't upload image in edit method.我是新学习 codeigniter 3,我有问题,我无法在编辑方法中上传图像。 i'm really stuck, i already figure it out from the internet and documentation but there is no answer and my code still not working, when click submit button the method was success update to database but not with the image, here is my code我真的被卡住了,我已经从互联网和文档中弄清楚了,但是没有答案,我的代码仍然无法正常工作,当单击提交按钮时,方法是成功更新到数据库但没有图像,这是我的代码

My Controller我的 Controller

public function edit()
    {
        $data['title'] = 'Edit Profile';
        $data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array(); //get username

        $this->form_validation->set_rules('name', 'Full Name', 'required|trim');

        if ($this->form_validation->run() == false) {

            $this->load->view('templates/header', $data);
            $this->load->view('templates/sidebar', $data);
            $this->load->view('templates/topbar', $data);
            $this->load->view('user/edit', $data);
            $this->load->view('templates/footer');
        } else {

            $name = $this->input->post('name');
            $email = $this->input->post('email');

            //cek jika ada gambar yg akan diupload
            $upload_image = $_FILES['image']['name'];

            if ($upload_image) {
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size']     = '2048';
                $config['upload_path'] = './assets/img/profile/';

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

                if ($this->upload->do_upload('image')) {
                    $new_image = $this->upload->data('file_name');
                    $this->db->set('image', $new_image);
                } else {
                    echo $this->upload->display_errors();
                }
            }


            $this->db->set('name', $name);
            $this->db->where('email', $email);
            $this->db->update('user');

            $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Your profile has been updated</div>');
            redirect('user');
        }
    }

My Views我的观点

            <?php echo form_open_multipart('user/edit'); ?>

            <div class="form-group row">
                <label for="email" class="col-sm-2 col-form-label col-form-label-sm">Email</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control form-control-sm" id="email" name="email" value="<?php echo $user['email']; ?>" readonly>
                </div>
            </div>
            <div class="form-group row">
                <label for="name" class="col-sm-2 col-form-label col-form-label-sm">Full Name</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control form-control-sm" id="name" name="name" value="<?php echo $user['name']; ?>">
                    <?php echo form_error('name', '<small class="text-danger pl-3">', '</small>'); ?>
                </div>
            </div>
            <div class="form-group row">
                <div class="col-sm-2">Picture</div>
                <div class="col-sm-10">
                    <div class="row">
                        <div class="col-sm-3">
                            <img src="<?php echo base_url('assets/img/profile/') . $user['image']; ?>" class="img-thumbnail">
                        </div>
                        <div class="col-sm-9">
                            <div class="custom-file">
                                <input type="file" class="custom-file-input" id="customFile">
                                <label class="custom-file-label" for="customFile">Choose file</label>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="form-group row justify-content-end">

                <div class="col-sm-10">
                    <button type="submit" class="btn btn-primary">Save</button>
                </div>
            </div>

You need to change with your code like this您需要像这样更改您的代码

Change view with name attribute in your file input tag.在文件input标签中使用name属性更改视图

<input type="file" class="custom-file-input" id="customFile"> to <input type="file" name="userfile" class="custom-file-input" id="customFile"> <input type="file" class="custom-file-input" id="customFile"><input type="file" name="userfile" class="custom-file-input" id="customFile">

and use </form> tag in last.最后使用</form>标签。

<?php echo form_open_multipart('user/edit'); ?>
    <div class="form-group row">
        <label for="email" class="col-sm-2 col-form-label col-form-label-sm">Email</label>
        <div class="col-sm-10">
            <input type="text" class="form-control form-control-sm" id="email" name="email" value="<?php echo $user['email']; ?>" readonly>
        </div>
    </div>
    <div class="form-group row">
        <label for="name" class="col-sm-2 col-form-label col-form-label-sm">Full Name</label>
        <div class="col-sm-10">
            <input type="text" class="form-control form-control-sm" id="name" name="name" value="<?php echo $user['name']; ?>">
            <?php echo form_error('name', '<small class="text-danger pl-3">', '</small>'); ?>
        </div>
    </div>
    <div class="form-group row">
        <div class="col-sm-2">Picture</div>
        <div class="col-sm-10">
            <div class="row">
                <div class="col-sm-3">
                    <img src="<?php echo base_url('assets/img/profile/') . $user['image']; ?>" class="img-thumbnail">
                </div>
                <div class="col-sm-9">
                    <div class="custom-file">
                        <input type="file" name="userfile" class="custom-file-input" id="customFile">
                        <label class="custom-file-label" for="customFile">Choose file</label>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="form-group row justify-content-end">

        <div class="col-sm-10">
            <button type="submit" class="btn btn-primary">Save</button>
        </div>
    </div>
</form>

And your controller change with bellow..而你的controller改变如下..

public function edit() {
    $data['title'] = 'Edit Profile';
    $data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array(); //get username

    $this->form_validation->set_rules('name', 'Full Name', 'required|trim');

    if ($this->form_validation->run() == false) {
       $this->load->view('templates/header', $data);
       $this->load->view('templates/sidebar', $data);
       $this->load->view('templates/topbar', $data);
       $this->load->view('user/edit', $data);
       $this->load->view('templates/footer');
    } else {
       $name = $this->input->post('name');
       $email = $this->input->post('email');

       $config['allowed_types'] = 'gif|jpg|png';
       $config['max_size']     = '2048';
       $config['upload_path'] = './assets/img/profile/';
       
       $this->load->library('upload', $config);

       if (!$this->upload->do_upload('userfile')) {
           echo $this->upload->display_errors();
        } else {
           $new_image = $this->upload->data('file_name');
           
           $this->db->set('image', $new_image);
           $this->db->set('name', $name);
           $this->db->where('email', $email);
           $this->db->update('user');

           $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Your profile has been updated</div>');
           
           redirect('user');
        }
    }
}

I am not familiar with codeigniter, so maybe some of this is happening in your method calls, but I think your main problem is related to $upload_image = $_FILES['image']['name'];我不熟悉 codeigniter,所以可能其中一些发生在您的方法调用中,但我认为您的主要问题与$upload_image = $_FILES['image']['name']; which only contains the file's name and no actual data.它只包含文件名,没有实际数据。

  1. the ['image'] part says the file was posted by the element named 'image' so in your html you need to name the file input with name="image" . ['image'] 部分表示文件是由名为 'image' 的元素发布的,因此在您的 html 中,您需要使用name="image"命名文件输入。
  2. the uploaded file's actual data get stored as a file in php's tmp folder with a randomized name, so to access it you would need to use $_FILES['image']['tmp_name'] and move it to your desired location (presumably ./assets/img/profile/ ) using move_upload_file() before it will actually be considered "uploaded" to your server.上传文件的实际数据以随机名称存储在 php 的tmp文件夹中,因此要访问它,您需要使用$_FILES['image']['tmp_name']并将其移动到您想要的位置(大概是./assets/img/profile/ ) 使用move_upload_file()之前它实际上将被视为“上传”到您的服务器。 See here and here .这里这里
  3. File uploads need rigorous validation to prevent malicious uploads, so validate both the user supplied name string $_FILES['images']['name'] and the file data itself.文件上传需要严格验证以防止恶意上传,因此请验证用户提供的名称字符串$_FILES['images']['name']和文件数据本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM