Warning: strpos() expects parameter 1 to be string, resource

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

I recently migrated a PHP site to my server and after migration I receive this error message. As I'm not really familiar with PHP, I'd really appreciate any help. Thanks.

Warning: strpos() expects parameter 1 to be string, resource given in .../public_html/store /product_list.php on line 121

Line 121 is as follows...

$exists = (strpos($handle, "Resource id") !== false) ? true : false; 

Here is the rest of the code on the top of the page for relevance.

<?php session_start(); include_once("../includes/define.inc.php"); include("../includes/common.php"); include("../includes/mysql_functions.php");  if( isset( $_GET['category'] ) ) {     $exists = checkIfExists("aw_category", "aw_category_urlalias='". $_GET['category']."'", "aw_category_id");     if( !$exists )     {         header("Location: " . PRODUCT_LIST );     } }   $get_category = ( isset( $_GET['category'] ) ) ? $_GET['category'] : ""; $category_id = ( $get_category == "" ) ? "" : getCategoryIDByAlias( $get_category ); $get_page = (isset($_GET['page']) ) ? $_GET['page'] : 0;   /*category menu*/ $qry_cat = "SELECT aw_category_urlalias, aw_category_id,aw_category_name,aw_category_order,aw_category_status FROM aw_category WHERE aw_category_status = 1 ORDER BY aw_category_order asc"; $result_cat = Query($qry_cat);  /*product*/ $qry_pro = "SELECT *             FROM aw_product             INNER JOIN aw_category             ON aw_product.aw_product_category = aw_category.aw_category_id             INNER JOIN aw_image             ON aw_product.aw_product_id = aw_image.aw_img_prodid             WHERE aw_product.aw_product_status = 1";  if( $category_id == "" )  { //Feature Product     $qry_pro .= " AND aw_product.aw_product_category = 1"; } else {     $qry_pro .= " AND aw_product.aw_product_category = ".$category_id.""; }  $qry_pro .= " GROUP BY aw_product.aw_product_id              ORDER BY aw_product.aw_product_priority desc,aw_product.aw_product_date desc";  if( $get_category=="" )  { //Feature Product     $qry_pro .= " LIMIT 6"; }  $result_pro = Query( $qry_pro ); //$row_pro = mysql_fetch_array($result_pro); $result_pro2 = Query( $qry_pro );  if( !$get_category == "" )  { /*Pagination*/ $num_per_page= 12; $num_rows = mysql_num_rows($result_pro);  $num_pages = ceil($num_rows/$num_per_page); $nav = "";   $begin = $get_page * $num_per_page;  $qry_pro .= " LIMIT " . $begin . ",12"; $result_pro = Query( $qry_pro ); $row_pro = mysql_fetch_array($result_pro);  if( $get_page > 0 )  {     $nav ="<a class=\"page_a\" href=\"".PRODUCT_LIST."?category=".$get_category."&page=".( $get_page-1 )."\">&laquo; Previous</a> | "; }  for($p=0;$p<$num_pages;$p++)  {     if($get_page == $p)         $nav .="<a class=\"page_a\" style='text-decoration:underline' href=\"".PRODUCT_LIST."?category=".$get_category."&page=".$p."\">".($p+1)."</a> | ";     else         $nav .="<a class=\"page_a\" href=\"".PRODUCT_LIST."?category=".$get_category."&page=".$p."\">".($p+1)."</a> | "; } if($get_page<$num_pages-1) {     $nav .="<a class=\"page_a\" href=\"".PRODUCT_LIST."?category=".$get_category."&page=".($get_page+1)."\"> Next &raquo;</a>"; } }//------- /*news*/ $qry_news = "SELECT aw_news_title FROM aw_news ORDER BY aw_news_date desc LIMIT 8"; $result_news = Query($qry_news);     function getCategoryIDByAlias( $alias ) {     $query = "SELECT aw_category_id FROM aw_category WHERE aw_category_urlalias='".$alias."'";     $rs = Query( $query );     $row = mysql_fetch_array( $rs );     return $row['aw_category_id']; }  function checkIfThumbExists( $thumb ) {      //$exists = ( file_exists( $img_src_thumb ) ) ? true : false;     //echo $exists;     //$exists = ( is_file( $img_src_thumb )  ) ? true : false;     //echo $exists;       //$AgetHeaders = @get_headers( $img_src_thumb );     //$exists = ( preg_match( "|200|", $AgetHeaders[0] ) ) ? true : false;     //echo $exists;       //$header_response = get_headers($img_src_thumb, 1);     //$exists = ( strpos( $header_response[0], "404" ) !== false ) ? false : true;;      //echo $exists;       $handle = @fopen($thumb, 'r');     $exists = (strpos($handle, "Resource id") !== false) ? true : false;      if( $exists )     {         $size = getimagesize( $thumb );          if( $size[3] == 'width="214" height="214"')         {             $exists = true;         } else {             $exists = false;         }      }     return $exists; }   ?> 

回答1:

Try replacing line 121 with the following:

$handle = @file_get_contents($thumb); 


回答2:

$handle = @fopen($thumb, 'r'); 

$handle not is string



回答3:

The error is clear. Read manual of stropos()

It need to take a string in parameter, but in you case you set there one source($handle = @fopen($thumb, 'r');) and one string("Resource id")

Use file_get_contents, as example.



回答4:

fopen returns a resource and strpos expects the first parameter to be a string.

You may use file_get_contents instead, but are you sure you want to check the binary data of a image?

$data = file_get_contents($thumb); 


回答5:

I don't know what are you trying to do with this line, but if you want to weather the file exists or not, i recommend you to use the native PHP functión file_exists, that gives you the chance to check if the file exists or not:

$exists = file_exists($thumb) 

Here is PHP reference.

http://es1.php.net/manual/es/function.file-exists.php



回答6:

As mentioned in other answers you are giving strpos a file handle or 'resource', which is wrong.

However, it looks like you want to test is if the file exists so I would simply do:

$handle = @fopen($thumb, 'r'); if($handle) {    // File exists } else {    // File doesn't exist } 

As fopen() will return a pointer (resource) if the file can be opened, else false if not.

file_get_contents() looks like the wrong option as it appears you are trying to open and image, so why would you want to search the binary for a string.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!