2009-02-06 37 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的原始值是不是最好的主意和此外,该方法不考虑被重定向到错误页面等。