上传文件无法在codeigniter中工作3错误:您没有选择任何文件

时间:2017-10-20 10:07:55

标签: php codeigniter view controller codeigniter-3

我创建了根目录并设置了它的路径,我按原样完成了前端。 ide没有给出任何错误但是当我继续说它你没有选择任何图像。

控制器代码:

class Images extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $this->data['displayname'] = $this->admin->lastname;
        $this->data['images'] = Image::all();
        $this->data['btn'] = 'Save';
        $this->data['content'] = 'admin/images/index';
        $this->load->view('layouts/admin', $this->data);

    }
    public function do_upload() {
        $config['upload_path']   = './uploads/';
        $config['allowed_types'] = '*';
        //$config['max_size']      = 100;
        //$config['max_width']     = 1024;
        //$config['max_height']    = 768;
        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload('images'))
        {
            $error = array('error' => $this->upload->display_errors());
             // echo $this->upload->display_errors();
            $this->load->view('index', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

            $this->load->view('upload_success', $data);
        }

    }
}

视图代码:这是选择文件的形式

 <?= form_open('admin/images/do_upload') ?>
            <label><?= form_upload('images[]')?></label>
            <div class="input-group-btn">
                <button class="btn btn-success add-image" type="button"><i class="glyphicon glyphicon-plus"></i> Add</button>
            </div>
        </div>

    </ul>

    <div class="line"></div>

<?= form_submit('', 'Submit', 'class="publish-btn"') ?>

<?= form_close()?>

4 个答案:

答案 0 :(得分:1)

<?= form_open_multipart('admin/images/do_upload') ?>

使用上述代码代替

<?= form_open('admin/images/do_upload') ?>

答案 1 :(得分:0)

在文件上传案例中,您应该使用

<?php form_open_multipart('admin/images/do_upload') ?>

不是这个

<?php  form_open('admin/images/do_upload') ?>

答案 2 :(得分:0)

我将多部分添加到我的表单中,这会创建一个无效路径问题,我通过在添加

之后初始化库来解决
$this->upload->initialize($config);

在这行加载库之后。

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

首先加载库然后初始化。 但仍然没有解决选择的问题 我也尝试了其他一些代码。

答案 3 :(得分:0)

经过大量研究并尝试了大量代码之后,我解决了我的问题 这是完整的事情 控制器代码:

 public function create($id)
    {
        $config ['upload_path'] = 'uploads';
        $config ['allowed_types'] = 'gif|jpg|png';
        $config ['encrypt_name'] = TRUE;

        $this->upload->initialize($config);
        $package = Package::find($id);

        $files = $_FILES;
        $cpt = count($_FILES ['images'] ['name']);

        for ($i = 0; $i < $cpt; $i++) {

            $_FILES ['images'] ['name'] = $files ['images'] ['name'] [$i];
            $_FILES ['images'] ['type'] = $files ['images'] ['type'] [$i];
            $_FILES ['images'] ['tmp_name'] = $files ['images'] ['tmp_name'] [$i];
            $_FILES ['images'] ['error'] = $files ['images'] ['error'] [$i];
            $_FILES ['images'] ['size'] = $files ['images'] ['size'] [$i];
            if ($this->upload->do_upload('images')){
                $upload_data = $this->upload->data();
                $file_name = $upload_data['file_name'];
                $package->create_images(array('image_location' => $file_name));
            }
        }

        redirect('admin/packages/');
    }
}

观看代码:

<div class="container">
    <?= form_open_multipart('admin/images/create/' . $this->uri->segment(4)) ?>
    <div class="row">
        <div class="col-md-8 publish">
            <h4>Image Gallery</h4>
            <div class="line"></div>
            <ul>
                <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>



                    <div class="input-group control-group after-add-image">


                        <label><input type="file" name="images[]"></label>

                        <div class="input-group-btn">
                            <button class="btn btn-success add-image" type="button"><i class="glyphicon glyphicon-plus"></i> Add</button>
                        </div>
                    </div>

                </ul>

                <div class="line"></div>

            <?= form_submit('', 'Submit', 'class="publish-btn"') ?>

            <?= form_close()?>
            <div class="clear-all"></div>
            <!-- Copy Fields-These are the fields which we get through jquery and then add after the above input,-->
            <div class="copy-field" style="display: none">
                <div class="control-group input-group" style="margin-top:10px">
                    <label><label><input type="file" name="images[]"></label></label>
                    <div class="input-group-btn">
                        <button class="btn btn-danger delete" type="button"><i class="glyphicon glyphicon-delete"></i> Remove</button>
                    </div>
                </div>
            </div>
            <script type="text/javascript">
                $(document).ready(function() {

                    //here first get the contents of the div with name class copy-fields and add it to after "after-add-more" div class.
                    $(".add-image").click(function(){
                        var html = $(".copy-field").html();
                        $(".after-add-image").after(html);
                    });
//here it will remove the current value of the remove button which has been pressed
                    $("body").on("click",".delete",function(){
                        $(this).parents(".control-group").remove();
                    });

                });
            </script>

动态获取所有图像,将它们上传到目录,然后将路径存储到数据库  完成了!

the image for front end