Yii2:当进程 Excel 包含 90k 行时 PHPExcel 出错

Yii2: PHPExcel error when process Excel contain 90k rows

我有一个具有上传 Excel 文件进程的应用程序。然后PHPExcel会处理上传的Excel文件。

这是流程:

我已成功将 Excel 文件保存到本地目录,从 Excel 文件读取并获取数据。

但是当我使用包含 90K 行 x 25 列 的 Excel 文件时,该文件没有保存到本地目录中。然后我尝试 print_r 数据,它 return error=> 1

这是 print_r 结果

1. Excel 少于 8k 行并成功保存到本地目录:

2。 Excel 有 90K 行并且没有保存到本地目录:

这是我在控制器中的代码

public function actionCreate() {
    $connection = \Yii::$app->db;
    $model = new MasterProduct();
    $userId = Yii::$app->user->id;

    $date = new DateTime('now', new \DateTimeZone('Asia/Bali'));
    $created_at = $date->format('Y:m:d H:i:s');

    $dirTrash = Yii::getAlias('@webroot/trash');
    $dirSubTrash = Yii::getAlias('@webroot/trash/trash_app');

//create directory in local if doesn't exist
    if (!is_dir($dirTrash)) {
        mkdir(Yii::getAlias('@webroot/trash'));
        if (!is_dir($dirSubTrash)) {
            mkdir(Yii::getAlias('@webroot/trash/trash_app'));
        }
    } else {
        if (!is_dir($dirSubTrash)) {
            mkdir(Yii::getAlias('@webroot/trash/trash_app'));
        }
    }

    if (Yii::$app->request->post()) {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $result = [];
        $fileMaster = UploadedFile::getInstancesByName('master_product');
        $middleName = substr(md5(microtime() * 100000), rand(0, 9), 5);

    //named the uploaded file, then save into local directory
        if ($fileMaster !== null) {
            $nameMaster = $userId . '_MP' . '_' . $middleName . '_' . date('Y-m-d') . '_' . $fileMaster[0]->getBaseName() . "." . $fileMaster[0]->getExtension();
            $pathMaster = Yii::getAlias('@webroot/trash/trash_app/') . $nameMaster;
            $fileMaster[0]->saveAs($pathMaster);
            
        } else {
            $error[] = "Choose the" . "<strong>" . " Excel " . "</strong>" . "file first.";
            $result = [
                'error' => $error
            ];
        }

    //this the code i've used to print_out the array in above images
        echo '<pre>';
        print_r($fileMaster);
        die();  

    //Access, and get the data from local directory
        $objPHPExcelMaster = new \PHPExcel();
        $fileNameMaster = Yii::getAlias('@webroot/trash/trash_app/') . $nameMaster;
        $inputFilesMaster = fopen(Yii::getAlias('@webroot/trash/trash_app/') . $nameMaster, "r");
        try {
            $inputFileTypeMaster = \PHPExcel_IOFactory::identify($fileNameMaster);
            $objReaderMaster = \PHPExcel_IOFactory::createReader($inputFileTypeMaster);
            $objPHPExcelMaster = $objReaderMaster->load($fileNameMaster);
        } catch (Exception $ex) {
            die('Error');
        }
        $sheetMaster = $objPHPExcelMaster->getSheet(0);
        $highestRowMaster = $sheetMaster->getHighestDataRow();
        $highestColumnMaster = $sheetMaster->getHighestDataColumn();
     
    //read the Excel file and set into array
        for ($row = 1; $row <= $highestRowMaster; ++$row) {
            $rowDataMaster = $sheetMaster->rangeToArray('A' . $row . ':' . $highestColumnMaster . $row, NULL, TRUE, NULL);
        }

        $tempDataMaster = mysql_escape_string(json_encode($rowDataMaster));

        $commandCheckExist = "SELECT COUNT(user_id) FROM `gm_json_master` WHERE id=$userId";
        $queryCheckExist = Yii::$app->db->createCommand($commandCheckExist)->execute();         
    
    //save data into db
        if ($queryCheckExist == 0) {
            $command_2 = "INSERT INTO json_master(json_m_product) VALUES('$tempDataMaster')";
            $query_2 = Yii::$app->db->createCommand($command_2)->execute();
        } else {
            $commandUpdate = "UPDATE json_master SET json_m_product='$tempDataMaster'";
            $queryUpdate = Yii::$app->db->createCommand($commandUpdate)->execute();
        }

        $result = [
            'url' => Url::to(['/gm/json-master-product/save'])
        ];
        return $result;
    } else {
        return $this->render('create', [
                    'model' => $model,
        ]);
    }
}

这是怎么发生的?以及如何使用 PHPExcel?

读取 Excel 的大数据

错误代码 1 表示文件大小超过了服务器配置允许的最大文件大小。

来自docs

UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

我会尝试提高该限制以及最大 POST 大小限制,看看是否有帮助。