警告:substr()要求参数1为字符串,给定数组

时间:2011-06-29 00:18:15

标签: php image function

  

可能重复:
  mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

我正在尝试使用the second function in this post来检查URL是否是PHP中的图像。我让它在运行WAMP的家用电脑上运行,但是当我将它上传到我的webhost时,它给了我两个错误:

警告:substr()要求参数1为字符串,在第22行的... / checkifimage.php中给出数组

警告:substr()要求参数1为字符串,资源在第22行的... / checkifimage.php中给出

1 个答案:

答案 0 :(得分:2)

stream_get_meta_data返回的数组中的wrapper_data条目被定义为混合,我认为你不能假设它将包含什么。它可能包含NULL条目或其他数组......因为你明确想要找到一个字符串:

if(is_array($wrapper_data)){
  foreach(array_keys($wrapper_data) as $hh){
      if (is_string($wrapper_data[$hh]) &&
          substr($wrapper_data[$hh], 0, 19) == "Content-Type: image") // strlen("Content-Type: image") == 19 
      {
        fclose($fp);
        return true;
      }
  }
}

会处理这个问题...但我同意var_dump()看看你传递的确切内容会帮助你解决问题。