9

I've just updated my phpexcel to phpspreadsheet, and I noticed this error pops up:

ErrorException (E_DEPRECATED) Array and string offset access syntax with curly braces is deprecated

require 'Classes/PHPExcel.php';

here is part of my code which is triggering the above error:

File: project/public/Classes/PHPExcel/Shared/ZipStreamWrapper.php

  public function stream_open($path, $mode, $options, &$opened_path)
    {
        // Check for mode
        if ($mode{0} != 'r') { //Error Line
            throw new PHPExcel_Reader_Exception('Mode ' . $mode . ' is not supported. Only read mode is supported.');
        }
 

File: project/public/Classes/PHPExcel.php

/** PHPExcel root directory */
if (!defined('PHPEXCEL_ROOT')) {
    define('PHPEXCEL_ROOT', dirname(__FILE__) . '/');
    require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php'); //Error Line
}

File: app/Http/Controllers/analyticsAuth/statement.old.php

use PHPExcel_Reader_Excel2007;
use PHPExcel; 
use PHPExcel_IOFactory;
use ZipArchive;
require 'Classes/PHPExcel.php'; //Error Line

File: project/public/Classes/PHPExcel/Autoloader.php

PHPExcel_Autoloader::Register();
PHPExcel_Shared_ZipStreamWrapper::register(); //Error Line
if (ini_get('mbstring.func_overload') & 2) {
    throw new Exception('Multibyte function overloading in PHP must be disabled for string functions (2).');
}

Thank you

Maqsud
  • 739
  • 3
  • 12
  • 35
  • You probably need your `require` to be before the other `use` statements. Are you sure `Classes/PHPExcel.php` exists? If you replace `require` with `include` and the error goes away, it means the file can't be found. – HeySora Aug 27 '20 at 08:22
  • @HeySora, I haven't removed PHPExcel yet. – Maqsud Aug 27 '20 at 08:46
  • But I'm trying to Migrate from `phpexcel` to `phpspreadsheet`. – Maqsud Aug 27 '20 at 08:47
  • Thanks for adding the actual error to your question. An `E_DEPRECATED` error means some piece of code used to be valid in a previous PHP version, but isn't valid anymore. You should have details under the error in order to locate the file name as well as the line number. – HeySora Aug 27 '20 at 08:48
  • @HeySora, I've added detailed of the following Error. – Maqsud Aug 27 '20 at 08:58
  • You need to go further in `PHPExcel_Shared_ZipStreamWrapper`. – HeySora Aug 27 '20 at 09:01
  • Added that too. Please check and let me know if anything else is required. – Maqsud Aug 27 '20 at 09:05
  • The error is ` Array and string offset access syntax with curly braces is deprecated`. The code that throws the error is `$mode{0}`. It needs to be replaced with `$mode[0]`. Repeat this operation for any remaining error, and you'll be done. – HeySora Aug 27 '20 at 09:09
  • If you are in laravel and not using Maatwebsite/Laravel-Excel package you are doing something wrong imo – mrhn Aug 27 '20 at 09:10
  • @HeySora, Thanks Got the answer. – Maqsud Aug 27 '20 at 09:20
  • This library already depreciated. You can use PHPOffice/PhpSpreadsheet instead – Yellow Digital Labs Aug 29 '22 at 11:33

2 Answers2

12

This can be fix by replacing the curly braces {} with square brackets []

I would like to give credit to the @HeySora who made the comment and pointed out the exact issue in this particular case.

Maqsud
  • 739
  • 3
  • 12
  • 35
  • 3
    Where had you changed the bracket? Can you share the code? – user9437856 Dec 12 '20 at 17:56
  • Everyone should be advised that PHPExcel is a DEAD repository and project and the code is depricated and not being maintained in any way at all. Do not attempt to try and update it, it is a fool's errand. Easier Instead to consider using PhpSpreadsheet, which is a fine replacement. I was able to only change several lines of code where I just point to it instead of the PhpExcell in my code, and also I just replaced PhpExcell's wording with PhpSpreadsheets version of that, and viola! code worked fine. Also use PhpSpreadsheets instruction to install it. works fine using composer. – Norman Bird Apr 22 '23 at 23:15
3

This is not longer in use $mode{0} in php 7. it has been deprecated. Instead of using that use this $mode[0]

Chibueze Agwu
  • 910
  • 5
  • 12