nginx PHP消息:PHP致命错误:未捕获的类型错误:无法访问字符串[duplicate]上字符串类型的偏移量

2fjabf4q  于 2022-12-03  发布在  Nginx
关注(0)|答案(5)|浏览(218)

此问题在此处已有答案

Illegal string offset Warning PHP(17个答案)
6个月前关闭。
我得到以下错误:

PHP message: PHP Fatal error: Uncaught TypeError: Cannot access offset of type string on string

在这一行:

if ($uploadZoneData[1]['size'] != 0) {

在php7.4上我有任何麻烦,但在php8上我有。
有什么问题吗?
EDIT:相关函数的完整代码:

function uploadSingleFile($zoneImage, $fileMoveTo, $fileAllowedExtensions, $fileAllowedSize, $zoneCustomer, $zone)
{
    global $config;

    // define file upload settings
    $errors = array();
    $fileName = $zoneImage['name'];
    $fileSize = $zoneImage['size'];
    $fileTmp = $zoneImage['tmp_name'];
    $fileType = $zoneImage['type'];
    $fileExt = strtolower(end(explode('.', $zoneImage['name'])));
    $MBtoByte = $fileAllowedSize * 1048576;
    $extensions= $fileAllowedExtensions;

    // define errors
    if (in_array($fileExt, $extensions) === false) {
        $errors[] = "Dieser Datei-Typ ist nicht erlaubt";
    }

    if ($fileSize > $MBtoByte) {
        $errors[] = 'Die Datei ist zu gross';
    }

    // finally try to upload the file
    if (empty($errors) == true) {
        $temp = explode(".", $zoneImage["name"]);
        $newfilename = $zoneCustomer . '-' . strtoupper($zone) . '-' . uniqid() . '.' . end($temp);

        move_uploaded_file($fileTmp, $_SERVER["DOCUMENT_ROOT"] . $fileMoveTo . $newfilename);
        $status = '1';
    } else {
        $status = '0';
    }

    // build array of the different outputs
    $uploadStatus = array($status, $newfilename, $errors);

    return $uploadStatus;
}

function updateZoneData($zoneFile, $zoneCustomer, $zone, $zoneLink, $maxWidth, $bannerID)
{
    global $db;

    // get customer values
    $getCustomerValues = getColumnValue('customers', "WHERE `customerNr` = '" . $zoneCustomer . "'");

    // define redirect url
    switch ($zone) {
      case "a1":
        $redirectZone = "zones.php#zones-overview-a1-overview";
      break;
      case "a2":
        $redirectZone = "zones.php#zones-overview-a2-overview";
      break;
      case "b1":
        $redirectZone = "zones.php#zones-overview-b1-overview";
      break;
      case "b2":
        $redirectZone = "zones.php#zones-overview-b2-overview";
      break;
      case "b3":
        $redirectZone = "zones.php#zones-overview-b3-overview";
      break;
      case "b4":
        $redirectZone = "zones.php#zones-overview-b4-overview";
      break;
      case "a9":
        $redirectZone = "zones.php#zones-overview-a9-overview";
      break;
      case "a9-1":
        $redirectZone = "zones.php#zones-overview-a9-1-overview";
      break;
      case "a11":
        $redirectZone = "zones.php#zones-overview-a11-overview";
      break;
      default:
        $redirectZone = "zones.php";
      }

    // upload file to the server
    $uploadZoneData = uploadSingleFile($zoneFile, '/adserver/banners/', array("jpg", "jpeg", "png", "gif"), '3', $zoneCustomer, $zone);

    if ($uploadZoneData[1]['size'] != 0) {
        if ($uploadZoneData[0] == '1') {

            // create ZIP-Backup (zone-banners) from '/adserver/banners' to '/cp/includes/files/zip-backups'
            createZipBackup('/adserver/banners', '/cp/includes/files/zip-backups', 'adserver-banners.zip');

            // get zone values & delete old bannerImg from file-system
            $getZoneDeleteValues = getColumnValue('zones', "WHERE `customerNr` = '" . $zoneCustomer . "' AND `zone` = '" . $zone . "' AND `id` = '" . $bannerID . "'");
            unlink($_SERVER["DOCUMENT_ROOT"] . '/adserver/banners/' . $getZoneDeleteValues['0']['bannerImg']);

            // execute action
            $updateZoneData = $db->update("zones", [
                                                   "customerNr" => $zoneCustomer,
                                                   "customer" => $getCustomerValues['0']['customer'],
                                                   "zone" => $zone,
                                                   "bannerImg" => $uploadZoneData[1],
                                                   "bannerLink" => $zoneLink,
                                                   "maxWidth" => $maxWidth,
                                                   "changeDate" => date("Y-m-d H:i:s")
                                                   ], [
                                                      "id" => $bannerID
                                                      ]);

            redirectTo($redirectZone, 1, "« " . strtoupper($zone) . "-Banner (" . $getCustomerValues['0']['customer'] . " [K. N°: " . $zoneCustomer . "]) » wurde erfolgreich aktualisiert.", 'ZONES');
        } else {

        // collect and save errors (file-upload)
            $collectedErrors = array_flatten($uploadZoneData[2]);
            setcookie("collectedErrors", '1', time() + (1 * 5), "/"); // expire in 5 seconds
            $_SESSION["collectedErrors"] = $collectedErrors;

            redirectTo($redirectZone, 0, "« " . strtoupper($zone) . "-Banner (" . $getCustomerValues['0']['customer'] . " [K. N°: " . $zoneCustomer . "]) » konnte nicht aktualisiert werden.", 'ZONES');
        }
    } else {

        // execute action
        $updateZoneData = $db->update("zones", [
                                               "customerNr" => $zoneCustomer,
                                               "customer" => $getCustomerValues['0']['customer'],
                                               "zone" => $zone,
                                               "bannerLink" => $zoneLink,
                                               "maxWidth" => $maxWidth,
                                               "changeDate" => date("Y-m-d H:i:s")
                                               ], [
                                                  "id" => $bannerID
                                                  ]);

        redirectTo($redirectZone, 1, "« " . strtoupper($zone) . "-Banner (" . $getCustomerValues['0']['customer'] . " [K. N°: " . $zoneCustomer . "]) » wurde erfolgreich aktualisiert.", 'ZONES');
    }
}
doinxwow

doinxwow1#

首先,欢迎来到Stack Overflow!
此错误意味着您试图访问的字符串的索引[1]['size']无效。请确保uploadSingleFile(...)返回的是数组而不是字符串。
我检查了您的代码,发现uploadSingleFile返回的数组包含以下三项:

$uploadStatus = array($status, $newfilename, $errors);

$newfilename不是数组。它是一个字符串,如下所定义:

$newfilename = $zoneCustomer . '-' . strtoupper($zone) . '-' . uniqid() . '.' . end($temp);
dffbzjpn

dffbzjpn2#

谢谢你的回答我的问题的解决方法是。
替换此

if ($uploadZoneData[1]['size'] != 0) {

与以下内容一致:

if (!empty($uploadZoneData[1])) {
p8ekf7hl

p8ekf7hl3#

我不是一个技术人员,所以我发现自己经常在StackOverflow上寻找答案。我遇到了这个问题,来到这里寻找答案。我已经定义了数组($CA = [];在我的例子中),但是它在数组中比1更深的层次上失败了。在它的最深处,我的数组看起来像这样:

$CA[$row[2]][$row[3]][$row[4]][$row[5]]

我能找到的最简单的方法是必须定义数组的每一层,所以我在数据集的每个循环中都得到了这样的结果:(注意:如果$row[x]不为空,则所有$row[任何小于x的数字]都不会为空。)

WHILE ($row = $result->fetch_row()) {
    if ( !is_null($row[2]) && !is_array($CA[$row[2]]) ) { $CA[$row[2]] = []; }
    if ( !is_null($row[3]) && !is_array($CA[$row[2]][$row[3]]) ) { $CA[$row[2]][$row[3]] = []; }
    if ( !is_null($row[4]) && !is_array($CA[$row[2]][$row[3]][$row[4]]) ) { $CA[$row[2]][$row[3]][$row[4]] = []; }
    if ( !is_null($row[5]) && !is_array($CA[$row[2]][$row[3]][$row[4]][$row[5]]) ) { $CA[$row[2]][$row[3]][$row[4]][$row[5]] = []; }
  ... Code to set the array elements below ...

PHP不再自动创建您需要的数组深度;你必须明确地设置它以避免错误。希望这能让其他非技术人员在这个页面上更清楚。我对任何更好的方法很感兴趣(我喜欢保持我的代码简洁!)

zed5wv10

zed5wv104#

您可以使用

if(isset(...))

这是一个常见PHP8错误

ovfsdjhp

ovfsdjhp5#

你应该升级你的php。这会对你有帮助。因为很多代码在旧版本上不起作用。所以升级PHP会有帮助。否则分享你的完整代码。我们会尽快解决你的错误。

相关问题