PHPExcel错误:使用花括号的数组和字符串偏移访问语法已过时。

10

我刚刚将我的 phpexcel 更新为 phpspreadsheet,然后我发现出现了这个错误:

ErrorException (E_DEPRECATED) 采用大括号访问数组和字符串偏移量的语法已经被弃用

require 'Classes/PHPExcel.php';

这是我代码中引起上述错误的一部分:

文件: 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.');
        }
 

文件: 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
}

文件:app/Http/Controllers/analyticsAuth/statement.old.php


(翻译:该文件路径为“app/Http/Controllers/analyticsAuth/statement.old.php”)
use PHPExcel_Reader_Excel2007;
use PHPExcel; 
use PHPExcel_IOFactory;
use ZipArchive;
require 'Classes/PHPExcel.php'; //Error Line

文件: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).');
}

谢谢


@HeySora,我还没有移除PHPExcel。 - Maqsud
但我正在尝试从phpexcel迁移到phpspreadsheet - Maqsud
感谢您将实际错误添加到问题中。 E_DEPRECATED 错误意味着某些代码曾经在以前的 PHP 版本中是有效的,但现在不再有效。 在错误下面应该有详细信息,以便定位文件名和行号。 - HeySora
@HeySora,我已添加以下错误的详细信息。 - Maqsud
这个库已经过时了。您可以使用PHPOffice/PhpSpreadsheet代替。 - Yellow Digital Labs
显示剩余6条评论
2个回答

13

可以通过将花括号{}替换为方括号[]来解决此问题。

我想要感谢@HeySora,他在这个特定情况下发表了评论并指出了确切的问题。


3
你把括号改到哪里了?能分享一下代码吗? - user9437856
大家应该知道,PHPExcel是一个已经停止维护的项目,代码已经过时且不再得到任何维护。不要试图尝试更新它,这是愚蠢的行为。相反,考虑使用PhpSpreadsheet作为替代方案。我只需要更改几行代码,将其指向PhpSpreadsheet,同时将PhpExcell的措辞替换为PhpSpreadsheets的版本,就可以让代码正常工作了。安装也很简单,只需按照PhpSpreadsheets的说明使用composer即可。 - Norman Bird

3

在php 7中,$mode{0}已经不再使用,它已被弃用。请改用$mode[0]


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接