【问题标题】:My Input File Think the Input Is Still Empty Even Though I Have chosen A File我的输入文件认为输入仍然是空的,即使我选择了一个文件
【发布时间】:2021-08-23 04:22:39
【问题描述】:

我目前正在使用带有 Codeigniter 4 和 Bootstrap 4.6x 的上传系统。除了这个,一切都很好。所以我的想法是用户需要为我的排行榜网络应用上传截图。但问题是,我在测试上传图片(应该是图片)的时候,每次选择文件时,输入都不行,或者说输入没有收到我选择的文件。

这是我的表格:

<div class="row">
    <div class="col-8">
        <h1 class="my-3">Upload Highscore Anda!</h1>
        <form action="/highscore/upload_post" method="POST" enctype="multipart/form-data">
            <?= csrf_field(); ?>
            <div class="form-group row">
                <label for="score" class="col-sm-2 col-form-label">Score</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control <?= ($validation->hasError('score') ? 'is-invalid' : '' ); ?>" id="score" name="score" autofocus> 
                    <div class="invalid-feedback">
                        <?= $validation->getError('score'); ?>
                    </div>
                </div>
            </div>

<p>THE PROBLEM BELONGS HERE (i think)</p>
            <div class="form-group row">
                <label for="image" class="col-sm-2 col-form-label">Screenshot</label>
                <div class="col-sm-10">
                    <div class="custom-file">
                        <input type="file" class="custom-file-input  <?= ($validation->hasError('image') ? 'is-invalid' : '' ); ?>" name="image" id="image" multiple="multiple">
                        <div class="invalid-feedback">
                            <?= $validation->getError('image'); ?>
                        </div>
                        <label class="custom-file-label" for="image">Choose file</label>
                    </div>
                </div>
            </div>
<p>END OF PROBLEM</p>

            <div class="form-group row-4">
                <select name="game" class="custom-select mr-sm-2  <?= ($validation->hasError('game') ? 'is-invalid' : '' ); ?>" id="game">
                    <!-- yang diambil nanti valuenya. misal milih one, nanti kalo $game = $this->req->getVar('game'); hasilnya 1 -->
                    <option selected disabled>Pilih Game...</option> 
                    <?php foreach($games as $g) : ?>
                        <option value="<?= $g['id']; ?>"><?= $g['name']; ?></option>
                    <?php endforeach; ?>
                </select>
                <div class="invalid-feedback">
                    <?= $validation->getError('game'); ?>
                </div>
            </div>
            <div class="form-group row">
                <div class="col-sm-10">
                    <button type="submit" class="btn btn-primary">Post</button>
                </div>
            </div>
        </form>
    </div>
</div>

我的上传控制器: 由于某些错误,我在$this-&gt;req-&gt;getVar 中将request 更改为req

public function upload() {
        $games = $this->games_model->findAll();
        if ($this->session->get('logged_in') == true && $this->session->get('level') == 'user') {
            $data = [
                'title' => 'Upload',
                'session_data' => $this->session->get(),
                'games' => $this->games,
                'games' => $games,
                'validation' => $this->validation
            ];
            
            return view('upload', $data);   
        } 
        
        return redirect()->to('/login');
    }

    public function upload_post() {
        $score = $this->req->getVar('score');
        $image = $this->req->getFiles();
        $game = $this->req->getVar('game');

        if(!$this->validate([
            'score' => [
                'rules' => 'required',
                'errors' => [
                    'required' => 'Score tidak boleh kosong'
                ]
            ],
            'image' => [
                'rules' => 'required|uploaded[image]|max_size[image,5120]|is_image[image]|mime_in[image,image/jpg,image/jpeg,image/png]',
                'errors' => [
                    'required' => 'Screenshot tidak boleh kosong',
                    'uploaded' => 'Pilih gambar sampul terlebih dahulu',
                    'max_size' => 'Maksimal ukuran gambar adalah 5MB',
                    'is_image' => 'Yang Anda pilih bukan gambar',
                    'mime_in' => 'Yang Anda pilih bukan gambar'
                ]
            ],
            'game' => [
                'rules' => 'required',
                'errors' => [
                    'required' => 'Game tidak boleh kosong'
                ]
            ]
        ])) {
            return redirect()->to('/upload')->withInput();
        }
        
        
        // $this->post_model->save([
        //  'image' => $image,
        //  'score' => $score,
        //  'user_email' => $this->session->get('email'),
        //  'game_id' => $game
        // ]);
    }

提前谢谢你

【问题讨论】:

    标签: php codeigniter bootstrap-4


    【解决方案1】:

    这是一个工作文件上传的示例。

    <form class="form-horizontal" action="/" method="post" enctype="multipart/form-data">
        <input type="file" class="form-control" name="myFile" id="myFile" size="20">
        <button type="submit">
            Save
        </button>
    </form>

    php代码

    <?php
    
    $config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . "/your_path";
    $config['max_size'] = 0;
    $config['allowed_types'] = '*';
    // if you want to set custom file name
    $new_name = time() . $_FILES["medicalFile"]['name']; 
    $config['file_name'] = $new_name;
    // end file name set
    $this->load->library('upload', $config);
    $this->upload->do_upload('myFile');
    

    【讨论】:

    • 感谢您回答我的问题。原来我错了。在 CI4 中,如果要获取文件,请使用 getFile 而不是 getVar。我很抱歉我的错误
    【解决方案2】:

    改变这个 $image = $this->req->getFiles(); 到 $image = $this->req->getFiles('image');

    【讨论】:

      猜你喜欢
      • 2020-03-20
      • 2021-12-05
      • 1970-01-01
      • 2019-11-26
      • 2018-02-08
      • 1970-01-01
      • 2020-02-26
      • 2014-11-17
      • 1970-01-01
      相关资源
      最近更新 更多