2014-02-20 75 views
2

我最近將一個PHP站點遷移到了我的服務器,並在遷移後收到此錯誤消息。由於我不熟悉PHP,我非常感謝任何幫助。謝謝。警告:strpos()期望參數1是字符串,資源

警告:strpos()預計參數1是串,在給定的 ... /的public_html /存儲/product_list.php線121上121

行如下資源...

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

下面是關於相關性頁面頂部的其餘代碼。

<?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; 
} 


?> 
+0

'$ exists =(strpos($ handle,「Resource id」)!== false)? true:false;'WAT?!? - 爲什麼不只是測試一個布爾錯誤? RTFM! –

回答

0

嘗試用下面的替換線121:

$handle = @file_get_contents($thumb); 
+0

爲什麼要壓制警告?這是不好的做法。 – Harri

+0

同意。我添加它的唯一原因是,這是原始帖子中的原因。 –

+0

感謝您的所有意見,它可能不是最好的解決方案,但我粘貼上面的行,它解決了這個問題。 – user3331701

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

$處理不爲字符串

0

錯誤是顯而易見的。閱讀的stropos()

它需要一個字符串參數手冊,但你如果你設置有一個源($handle = @fopen($thumb, 'r');)和一個字符串(「資源ID」)

使用file_get_contents,爲例。

0

fopen返回一個資源,strpos期望第一個參數是一個字符串。

您可以使用file_get_contents代替,但是您確定要檢查圖像的二進制數據嗎?

$data = file_get_contents($thumb); 
0

我不知道什麼是你想用這條線做的,但如果你想度過文件存在與否,我建議你使用本地PHP函數file_exists,那給你有機會檢查文件是否存在:

$exists = file_exists($thumb) 

這裏是PHP參考。

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

-2

正如你給strpos文件句柄或「資源」,這是不對的其他的答案中提到。

但是,它看起來像你要測試的文件是否存在,所以我會簡單地做:

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

由於fopen()將返回一個指針(資源),如果文件可以被打開,如果其他錯誤不。

file_get_contents()看起來像錯誤的選項,因爲它似乎你正試圖打開和形象,所以你爲什麼要搜索二進制字符串。

+0

如果文件存在但是不可讀(這可能是爲了執行權限),那將會失敗。爲了測試一個文件是否存在,使用'file_exists()'。 – Timothy

+0

是的,但是他現在的代碼邏輯是一樣的,一張圖片沒有讀取權限的可能性很小。 – Springie

+0

如果文件在那裏,file_exists()會通過,但不測試它是否可讀,所以如果你想正確地做,你會做兩個檢查。 – Springie

相關問題