【问题标题】:PHP 7.0 Illegal offset type in isset or emptyPHP 7.0 isset 中的非法偏移类型或空
【发布时间】:2017-08-25 15:52:29
【问题描述】:

在我的 Apache ErrorLog 文件中,我多次看到此警告消息:

[2017 年 8 月 23 日星期三 17:27:25.146025] [:error] [pid 14989] [client 66.249.76.54:44935] PHP 警告:isset 中的偏移量类型非法或 /var/www/html/blog/ 中为空wp-content/plugins/ilab-media-tools/classes/tools/s3/ilab-media-s3-tool.php 在第 1012 行

它与一个 Wordpress 插件有关,我尝试使用 Github 向作者(和其他许多人一样)寻求帮助,但没有。

警告指向这个函数:

public function imageDownsize($fail,$id,$size) {
    if (apply_filters('ilab_imgix_enabled', false)) {
            return $fail;
    }

    $meta=wp_get_attachment_metadata($id);
    // This is the line of the warning:
    if (!isset($meta['sizes']) || !isset($meta['sizes'][$size])) {
            return $fail;
    }

    $sizeMeta = $meta['sizes'][$size];
    if (!isset($sizeMeta['s3'])) {
            return $fail;
    }

    $url = $sizeMeta['s3']['url'];

    $result=[
            $url,
            $sizeMeta['width'],
            $sizeMeta['height'],
        true
    ];

    return $result;
}

Apache 每分钟打印一次此警告。 有没有办法解决这个烦人的消息警告?

这是var_dump($meta);的输出:

array(7) {
  ["width"]=>
  int(140)
  ["height"]=>
  int(140)
  ["file"]=>
  string(31) "2016/01/logo_retina.png"
  ["sizes"]=>
  array(0) {
  }
  ["image_meta"]=>
  array(12) {
    ["aperture"]=>
    int(0)
    ["credit"]=>
    string(0) ""
    ["camera"]=>
    string(0) ""
    ["caption"]=>
    string(0) ""
    ["created_timestamp"]=>
    int(0)
    ["copyright"]=>
    string(0) ""
    ["focal_length"]=>
    int(0)
    ["iso"]=>
    int(0)
    ["shutter_speed"]=>
    int(0)
    ["title"]=>
    string(0) ""
    ["orientation"]=>
    int(0)
    ["keywords"]=>
    array(0) {
    }
  }
  ["ewww_image_optimizer"]=>
  string(30) "Reduced by 16.3% (1.3 kB)"
  ["s3"]=>
  array(3) {
    ["url"]=>
    string(79) "https://s3-eu-west-1.amazonaws.com/blog-example/2016/01/logo_retina.png"
    ["bucket"]=>
    string(12) "blog-example"
    ["key"]=>
    string(31) "2016/01/logo_retina.png"
  }
}
array(7) {
  ["width"]=>
  int(140)
  ["height"]=>
  int(140)
  ["file"]=>
  string(31) "2016/01/logo_retina.png"
  ["sizes"]=>
  array(0) {
  }
  ["image_meta"]=>
  array(12) {
    ["aperture"]=>
    int(0)
    ["credit"]=>
    string(0) ""
    ["camera"]=>
    string(0) ""
    ["caption"]=>
    string(0) ""
    ["created_timestamp"]=>
    int(0)
    ["copyright"]=>
    string(0) ""
    ["focal_length"]=>
    int(0)
    ["iso"]=>
    int(0)
    ["shutter_speed"]=>
    int(0)
    ["title"]=>
    string(0) ""
    ["orientation"]=>
    int(0)
    ["keywords"]=>
    array(0) {
    }
  }
  ["ewww_image_optimizer"]=>
  string(30) "Reduced by 16.3% (1.3 kB)"
  ["s3"]=>
  array(3) {
    ["url"]=>
    string(79) "https://s3-eu-west-1.amazonaws.com/blog-example/2016/01/logo_retina.png"
    ["bucket"]=>
    string(12) "blog-example"
    ["key"]=>
    string(31) "2016/01/logo_retina.png"
  }
}
array(7) {
  ["width"]=>
  int(140)
  ["height"]=>
  int(140)
  ["file"]=>
  string(31) "2016/01/logo_retina.png"
  ["sizes"]=>
  array(0) {
  }
  ["image_meta"]=>
  array(12) {
    ["aperture"]=>
    int(0)
    ["credit"]=>
    string(0) ""
    ["camera"]=>
    string(0) ""
    ["caption"]=>
    string(0) ""
    ["created_timestamp"]=>
    int(0)
    ["copyright"]=>
    string(0) ""
    ["focal_length"]=>
    int(0)
    ["iso"]=>
    int(0)
    ["shutter_speed"]=>
    int(0)
    ["title"]=>
    string(0) ""
    ["orientation"]=>
    int(0)
    ["keywords"]=>
    array(0) {
    }
  }
  ["ewww_image_optimizer"]=>
  string(30) "Reduced by 16.3% (1.3 kB)"
  ["s3"]=>
  array(3) {
    ["url"]=>
    string(79) "https://s3-eu-west-1.amazonaws.com/blog-example/2016/01/logo_retina.png"
    ["bucket"]=>
    string(12) "blog-example"
    ["key"]=>
    string(31) "2016/01/logo_retina.png"
  }
}
array(7) {
  ["width"]=>
  int(140)
  ["height"]=>
  int(140)
  ["file"]=>
  string(31) "2016/01/logo_retina.png"
  ["sizes"]=>
  array(0) {
  }
  ["image_meta"]=>
  array(12) {
    ["aperture"]=>
    int(0)
    ["credit"]=>
    string(0) ""
    ["camera"]=>
    string(0) ""
    ["caption"]=>
    string(0) ""
    ["created_timestamp"]=>
    int(0)
    ["copyright"]=>
    string(0) ""
    ["focal_length"]=>
    int(0)
    ["iso"]=>
    int(0)
    ["shutter_speed"]=>
    int(0)
    ["title"]=>
    string(0) ""
    ["orientation"]=>
    int(0)
    ["keywords"]=>
    array(0) {
    }
  }
  ["ewww_image_optimizer"]=>
  string(30) "Reduced by 16.3% (1.3 kB)"
  ["s3"]=>
  array(3) {
    ["url"]=>
    string(79) "https://s3-eu-west-1.amazonaws.com/blog-example/2016/01/logo_retina.png"
    ["bucket"]=>
    string(12) "blog-example"
    ["key"]=>
    string(31) "2016/01/logo_retina.png"
  }
}
array(7) {
  ["width"]=>
  int(140)
  ["height"]=>
  int(140)
  ["file"]=>
  string(31) "2016/01/logo_retina.png"
  ["sizes"]=>
  array(0) {
  }
  ["image_meta"]=>
  array(12) {
    ["aperture"]=>
    int(0)
    ["credit"]=>
    string(0) ""
    ["camera"]=>
    string(0) ""
    ["caption"]=>
    string(0) ""
    ["created_timestamp"]=>
    int(0)
    ["copyright"]=>
    string(0) ""
    ["focal_length"]=>
    int(0)
    ["iso"]=>
    int(0)
    ["shutter_speed"]=>
    int(0)
    ["title"]=>
    string(0) ""
    ["orientation"]=>
    int(0)
    ["keywords"]=>
    array(0) {
    }
  }
  ["ewww_image_optimizer"]=>
  string(30) "Reduced by 16.3% (1.3 kB)"
  ["s3"]=>
  array(3) {
    ["url"]=>
    string(79) "https://s3-eu-west-1.amazonaws.com/blog-example/2016/01/logo_retina.png"
    ["bucket"]=>
    string(12) "blog-example"
    ["key"]=>
    string(31) "2016/01/logo_retina.png"
  }
}

还有var_dump($size);的输出:

string(4) "full"
array(2) {
  [0]=>
  int(32)
  [1]=>
  int(32)
}
array(2) {
  [0]=>
  int(192)
  [1]=>
  int(192)
}
array(2) {
  [0]=>
  int(180)
  [1]=>
  int(180)
}
array(2) {
  [0]=>
  int(270)
  [1]=>
  int(270)
}

【问题讨论】:

  • 你确定$meta 是一个数组吗?
  • 你可以添加这一行,它应该可以工作if(count($meta["sizes"]) < $size) return $fail;
  • 把这一行放在下面$meta=wp_get_attachment_metadata($id);
  • 很高兴帮助@NineCattoRules...我也会在 github 上进行修复和拉取请求。
  • 根据你的var_dump$size是一个数组,导致偏移错误。此外,if(count($meta["sizes"]) < $size) return $fail; 在不满足 if 条件时仍会返回错误。你应该检查你传递给函数的$size

标签: php wordpress


【解决方案1】:

所以,根据 GitHub 问题,这条线似乎以某种方式失败了

$meta=wp_get_attachment_metadata($id);

根据WP reference返回

附件元字段。失败时为假。

代码错误地假设它总是会返回一个数组。因此,您可以通过更改以下内容来解决此问题,以确保 $meta 不为假

if (!$meta || !isset($meta['sizes']) || !isset($meta['sizes'][$size])) {
        return $fail;
}

【讨论】:

  • 好的,做一个var_dump($meta);然后看看它返回了什么
  • 哦,等等。也尝试做一个var_dump($size);。这可能是问题
  • 非法偏移类型表示偏移([ ]之间的值)无效。也就是说,它不是字符串、数字或布尔值。如果$size 是一个数组或者一个对象,就会产生这个警告。
  • @Hackerman 用if(count($meta["sizes"]) < $size) return $fail;解决了我的问题
猜你喜欢
  • 1970-01-01
  • 2019-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多