2009-02-06 90 views
0

我想查看是否存在URL,如果不存在,請將包含URL的變量重置爲錯誤頁面/圖像。不過,我的下面的嘗試將PIC_URL的每個實例都設置爲noimage.gif,而不管它是否存在並且可以訪問。我錯過了什麼?圖像URL錯誤捕獲問題

if (@fopen($_REQUEST[$PIC_URL],"r")) 
{ 
$status = "1"; 
} 
else if (!(@fopen($_REQUEST[$PIC_URL],"r")) 
{ 

$status = "2"; 
} 

if ($status == "2") $PIC_URL = "http://www.tricityhomes.com/assets/images/noimage.gif"; 

回答

0

測試尋找多餘的,你的意思$_REQUEST['PIC_URL']

if (@fopen($_REQUEST['PIC_URL'],"r")) 
{ 
    $status = "1"; 
} 
else 
{ 

    $status = "2"; 
} 

雖然有很多更多的錯誤。要測試一個遠程文件是否存在,最好使用file_exists,或者使用自己的方法來執行HEAD請求。

+0

仍然將每個圖像設置爲不存在的圖像。爲什麼會這樣做? – 2009-02-06 14:52:29

0

使用file_exists或is_readable,也不要在其他部分使用原始值

1

你可能想嘗試做類似這樣的

<?php 
function url_exists($url) { 
    // Version 4.x supported 
    $handle = curl_init($url); 
    if (false === $handle) 
    { 
     return false; 
    } 
    curl_setopt($handle, CURLOPT_HEADER, false); 
    curl_setopt($handle, CURLOPT_FAILONERROR, true); // this works 
    curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15")); // request as if Firefox 
    curl_setopt($handle, CURLOPT_NOBODY, true); 
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, false); 
    $connectable = curl_exec($handle); 
    curl_close($handle); 
    return $connectable; 
} 
?> 

來源的東西:http://uk.php.net/manual/en/function.file-exists.php#85246

的原因是是,作爲已經提到檢查對FOPEN的原始值是不是最好的主意和此外,該方法不考慮被重定向到錯誤頁面等。