带有规则扩展名的Codeigniter 4验证上传文件无法正常工作

问题描述

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

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

                            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文件并检查相应的mime类型时,请执行dd($ _ FILES)。

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