Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Import EXCEL happens exception at PHP7.4, response: Trying to access array offset on value of type int #2478

Closed
ldaaww opened this issue Dec 5, 2019 · 24 comments
Labels

Comments

@ldaaww
Copy link

ldaaww commented Dec 5, 2019

Versions

  • PHP version: PHP7.4 (Centos7 yum installed php74 form remisrepo stable version)
  • Laravel version: Laravel Framework 5.8.35
  • Package version: maatwebsite/excel 3.1.17

Description

Import EXCEL happens exception at PHP7.4,
Exception->message() is:
Trying to access array offset on value of type int.

Steps to Reproduce

Expected behavior:

import a xlsx file as i running the code on php72w

Actual behavior:
happened after I uploaded my server circumstance from php7.2 to php7.4 when I import a Excel would be fail like above.

would be normal again if I downgrade from php7.4 to php7.2.

Additional Information

$extension=$request->file()['file']->getClientOriginalExtension();
$extension[0] = strtoupper($extension[0]);
Excel::import(new RejectedImport(), $request->file()['file']->path(),null,$extension);

And what is in Import Class:

<?php

namespace App\Imports;

use App\Commodity;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class CommodityImport implements ToCollection, WithHeadingRow
{
    protected $isOverride=false;
    public function __construct($isOverride)
    {
        if($isOverride=='1')
            $this->isOverride=true;
    }

    /**
     * @param Collection $collections
     */
    public function collection(Collection $collections)
    {
        foreach ($collections as $row)
        {
            $barcode = $row['barcode'] ?? $row['BARCODE'] ?? $row['Barcode'];
            if(!$barcode)continue;
            $name = $row['name'] ?? $row['NAME'] ?? $row['Name'] ?? '';
            $sku = $row['sku'] ?? $row['SKU'] ?? $row['Sku'] ?? '';
            $owner = $row['owner'] ?? $row['owner_name'] ?? $row['OWNER'] ?? $row['Owner'] ?? '';
            $commodity=Commodity::where('barcode',$row['barcode'])->first();
            if($commodity){
                if($this->isOverride){
                    $name?$commodity['name']= $name:false;
                    $sku?$commodity['sku']= $sku:false;
                    $owner?$commodity['owner_name']= $owner:false;
                    $commodity->update();
                }
            }else{
                Commodity::create([
                    'name' => $name,
                    'sku' => $sku,
                    'owner_name' => $owner,
                    'barcode' => $barcode,
                ]);
            }
        }
    }
}
@ldaaww ldaaww added the bug label Dec 5, 2019
@ghost
Copy link

ghost commented Dec 5, 2019

Thanks for submitting the ticket. Unfortunately the information you provided is incomplete. We need to know which version you use and how to reproduce it. Please include code examples. Before we can pick it up, please check (https://github.com/Maatwebsite/Laravel-Excel/blob/3.1/.github/ISSUE_TEMPLATE.md) and add the missing information. To make processing of this ticket a lot easier, please make sure to check (https://laravel-excel.maatwebsite.nl/3.1/getting-started/contributing.html) and double-check if you have filled in the issue template correctly. This will allow us to pick up your ticket more efficiently. Issues that follow the guidelines correctly will get priority over other issues.

@ghost ghost removed the more information needed label Dec 5, 2019
@patrickbrouwers
Copy link
Member

Can you show a bit more stacktrace, I cannot confirm the bug when using the package on PHP7.4. Maybe it's something in your own code?

@mattmcardle
Copy link

Hi @patrickbrouwers thanks for the package, I'm a big fan of it!

I am getting a similar error after upgrading, although it is during an export, and the stacktrace indicates the problem is with phpsreadsheet, not Laravel-Excel, but google brought me here.

Here is the stacktrace:

#0 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/DefaultValueBinder.php(56): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError()
#1 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/DefaultValueBinder.php(34): PhpOffice\\PhpSpreadsheet\\Cell\\DefaultValueBinder::dataTypeForValue()
#2 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Cell.php(184): PhpOffice\\PhpSpreadsheet\\Cell\\DefaultValueBinder->bindValue()
#3 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Worksheet/Worksheet.php(2510): PhpOffice\\PhpSpreadsheet\\Cell\\Cell->setValue()
#4 /vendor/maatwebsite/excel/src/Sheet.php(402): PhpOffice\\PhpSpreadsheet\\Worksheet\\Worksheet->fromArray()
#5 /vendor/maatwebsite/excel/src/Sheet.php(502): Maatwebsite\\Excel\\Sheet->append()
#6 /vendor/maatwebsite/excel/src/Sheet.php(368): Maatwebsite\\Excel\\Sheet->appendRows()
#7 /vendor/maatwebsite/excel/src/Sheet.php(195): Maatwebsite\\Excel\\Sheet->fromCollection()
#8 /vendor/maatwebsite/excel/src/Writer.php(73): Maatwebsite\\Excel\\Sheet->export()
#9 /vendor/maatwebsite/excel/src/Excel.php(176): Maatwebsite\\Excel\\Writer->export()
#10 /vendor/maatwebsite/excel/src/Excel.php(97): Maatwebsite\\Excel\\Excel->export()
#11 /app/Console/Commands/EmailCardRequestList.php(89): Maatwebsite\\Excel\\Excel->store()

@mattmcardle
Copy link

Fixed in PhpSpreadsheet, see PHPOffice/PhpSpreadsheet#1300

@burello
Copy link

burello commented Mar 19, 2020

In the PHPExcel file "DefaultValueBinder.php", replace this line 82:
} elseif ($pValue[0] === '=' && strlen($pValue) > 1) {
with the following:
} elseif (0 === strpos($pValue, '=') && strlen($pValue) > 1) {
This will fix the "Trying to access array offset on value of type int" error. But you will also need to conduct a find and replace for curly braces throughout the PHPExcel code to address the "Array and string offset access syntax with curly braces is deprecated" error which also affects PHP ugrades to 7.4. I simply replaced them with "[" and "]" everywhere and everything is working fine again.

@jefcoserrca
Copy link

DefaultValueBinder.php inside of vendor folder? or where is it? same problem here.

@burello
Copy link

burello commented Mar 19, 2020

DefaultValueBinder.php inside of vendor folder? or where is it? same problem here.

My apologies. I should have been more specific. In my installation, it is located in the PHPExcel folder at this location: PHPExcel\Cell\DefaultValueBinder.php
It may vary from your installation. But you should be able to run a search for it on your system.

@jefcoserrca
Copy link

the line i think is the 62, not 82 my friend! and i hope that it will work 'cause im stress out. I cant find the solution to error in my project : Error ERROR: Trying to access array offset on value of type int '/Cell/DefaultValueBinder.php:56'. I have high hopes!

@faceretech
Copy link

This is the path : third_party/PHPExcel/Classes/PHPExcel/Cell/DefaultValueBinder.php
Here third_party means where you have kept your PHPExcel folder

Now search === '=' and check if there is matching line then replace with that particular to
0 === strpos($pValue, '=')
Rest would be same. In my case it was
$pValue[0] === '='

@zihad15
Copy link

zihad15 commented Apr 28, 2020

In the PHPExcel file "DefaultValueBinder.php", replace this line 82:
} elseif ($pValue[0] === '=' && strlen($pValue) > 1) {
with the following:
} elseif (0 === strpos($pValue, '=') && strlen($pValue) > 1) {
This will fix the "Trying to access array offset on value of type int" error. But you will also need to conduct a find and replace for curly braces throughout the PHPExcel code to address the "Array and string offset access syntax with curly braces is deprecated" error which also affects PHP ugrades to 7.4. I simply replaced them with "[" and "]" everywhere and everything is working fine again.

it works! love you!

@fredochieng
Copy link

In the PHPExcel file "DefaultValueBinder.php", replace this line 82:
} elseif ($pValue[0] === '=' && strlen($pValue) > 1) {
with the following:
} elseif (0 === strpos($pValue, '=') && strlen($pValue) > 1) {
This will fix the "Trying to access array offset on value of type int" error. But you will also need to conduct a find and replace for curly braces throughout the PHPExcel code to address the "Array and string offset access syntax with curly braces is deprecated" error which also affects PHP ugrades to 7.4. I simply replaced them with "[" and "]" everywhere and everything is working fine again.

When I was almost downgrading to PHP 7.3, thanks man this helped me

@ankitasoman
Copy link

ankitasoman commented Jul 20, 2020

Any idea to solve this issue with PHP 7.4.3 & maatwebsite/excel version 2.1?

@patrickbrouwers
Copy link
Member

You would have to fork the deprecated phpexcel package and maintain the fork yourself.

@lokanaft
Copy link

lokanaft commented Jul 20, 2020

} elseif (0 === strpos($pValue, '=') && strlen($pValue) > 1) {

strpos of bool, rly?

Just put this code after check to is_bool and is_float

@patrickbrouwers
Copy link
Member

Code is not part of this package, but of legacy PhpExcel

@virajkadam
Copy link

In the PHPExcel file "DefaultValueBinder.php", replace this line 82:
} elseif ($pValue[0] === '=' && strlen($pValue) > 1) {
with the following:
} elseif (0 === strpos($pValue, '=') && strlen($pValue) > 1) {
This will fix the "Trying to access array offset on value of type int" error. But you will also need to conduct a find and replace for curly braces throughout the PHPExcel code to address the "Array and string offset access syntax with curly braces is deprecated" error which also affects PHP ugrades to 7.4. I simply replaced them with "[" and "]" everywhere and everything is working fine again.

Thank You man. Loved it. Thankx @burello <3

@thomas4Bitcraft
Copy link

thomas4Bitcraft commented Aug 20, 2020

The best thing to do is to update the PHPOffice/PhpSpreadsheet above 1.10 in your project - because then this Error will be fixed without having to manually edit the package codebase. So for me

composer require phpoffice/phpspreadsheet "^1.10"

fixed the problem.

@3robi-vollebregt
Copy link

i have this e'rror too

@3robi-vollebregt
Copy link

al klakkel

lvandyk pushed a commit to UnitXOrg/PHPExcel that referenced this issue Oct 30, 2020
@tosswithim
Copy link

In the PHPExcel file "DefaultValueBinder.php", replace this line 82:
} elseif ($pValue[0] === '=' && strlen($pValue) > 1) {
with the following:
} elseif (0 === strpos($pValue, '=') && strlen($pValue) > 1) {
This will fix the "Trying to access array offset on value of type int" error. But you will also need to conduct a find and replace for curly braces throughout the PHPExcel code to address the "Array and string offset access syntax with curly braces is deprecated" error which also affects PHP ugrades to 7.4. I simply replaced them with "[" and "]" everywhere and everything is working fine again.

Thanks working for me !! orderimportexport.ocmod opencart 3x

@azmilassoued
Copy link

In the PHPExcel file "DefaultValueBinder.php", replace this line 82:
} elseif ($pValue[0] === '=' && strlen($pValue) > 1) {
with the following:
} elseif (0 === strpos($pValue, '=') && strlen($pValue) > 1) {
This will fix the "Trying to access array offset on value of type int" error. But you will also need to conduct a find and replace for curly braces throughout the PHPExcel code to address the "Array and string offset access syntax with curly braces is deprecated" error which also affects PHP ugrades to 7.4. I simply replaced them with "[" and "]" everywhere and everything is working fine again.

Amazing, you saved me bro, thank you so much

@fajarridikc
Copy link

replace this line 82:
} elseif ($pValue[0] === '=' && strlen($pValue) > 1) {
with the following:
} elseif (0 === strpos($pValue, '=') && strlen($pValue) > 1) {

working for me...

@volovodenko
Copy link
Contributor

image

@patrickbrouwers
Copy link
Member

Please report errors within PhpSpreadsheet to PhpSpreadsheet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests