繁体   English   中英

Codeigniter 4 具有规则扩展名的验证上传文件未按预期工作

[英]Codeigniter 4 validation upload file with rule extension is not work as expected

我有一个导入功能,这是我的代码

public function import($id = NULL)
    {
        if ($this->request->getMethod() == 'post') {
            if (empty($id) && $id != '0') {
                $rules = [
                    'template' => [
                        'label' => 'Template',
                        'rules' => 'uploaded[template]|ext_in[template,xlsx]'
                    ]
                ];

                if (!$this->validate($rules)) {
                    return $this->respond([
                        'type' => 'failed',
                        'message' => $this->validator->getError("template")
                    ]);
                }

                if ($file = $this->request->getFile('template')) {
                    if ($file->isValid() && !$file->hasMoved()) {
                        $newName = $file->getRandomName();
                        $file->move(WRITEPATH . "cache", $newName);

                        try {
                            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
                            $spreadsheet = $reader->load(WRITEPATH . "cache" . DIRECTORY_SEPARATOR . $newName);
                            $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);

                            if (count($sheetData) < 2) {
                                throw new \Exception("Unable to import empty files");
                            }

                            return $this->respond([
                                'type' => 'success',
                                'token' => $newName,
                                'rows' => count($sheetData) - 1
                            ]);
                        } catch (\Exception $e) {
                            return $this->respond([
                                'type' => 'failed',
                                'message' => $e->getMessage()
                            ]);
                        }
                    }
                }
            } else {
                $token = $this->request->getPost('token');
                $last = $this->request->getPost('last');
                $filePath = WRITEPATH . "cache" . DIRECTORY_SEPARATOR . $token;

                if (!$this->model->importExcel($filePath, $id)) {
                    return $this->respond([
                        'type' => 'failed',
                        'message' => 'Failed to import kpi',
                    ]);
                }

                if ($last == '1' && file_exists($filePath)) {
                    unlink($filePath);
                }

                return $this->respond([
                    'type' => 'success',
                    'message' => 'The kpi were imported successfully',
                ]);
            }
        }

        $data = [
            'formAction' => base_url("scorecard-tbcci/reporting/master-kpi/import"),
            'formMethod' => 'POST',
        ];

        echo view('ScorecardTbcci\Views\Reporting\Kpis\ImportFormView', $data);
    }

问题是,当我上传xlsx文件时。 此文件未通过验证。 将出现错误警告“上传没有有效的文件扩展名” 所以我尝试使用xls文件,我也更改了规则,但它是一样的。 当我用csv更改规则并上传 csv 文件时。 它通过了验证。 我不知道出了什么问题。 谁能帮我?

上传 xlsx 文件时执行 dd($_FILES) 并检查相应的 mime 类型。

然后将其与您的 app/config/mimes.php 进行比较,并检查 xlsx 数组中是否存在该 mime 类型。 也许您正在上传的文件具有配置文件中不存在的 MIME 类型。

暂无
暂无

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

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