2013-08-05 60 views
1

我想檢查服務器上是否存在圖像文件。當我獲得與其他服務器的圖像路徑。如何檢查圖像是否存在於服務器上使用URL

檢查下面的代碼,我都試過了,

$urlCheck = getimagesize($resultUF['destination']);

if (!is_array($urlCheck)) { 
    $resultUF['destination'] = NULL; 
} 

但是,它顯示警告

Warning: getimagesize(http://www.example.com/example.jpg) [function.getimagesize]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in 

有什麼辦法來那麼做?

謝謝。

+0

你需要給像「圖像/ example.jpg」 – PravinS

+0

絕對路徑@MakC是否有任何答案有幫助? –

+0

@sankalpMishra是的,我試着用下面的答案,但由於allow_url_fopen它顯示服務器上的警告,所以我已經嘗試使用CURL,它適用於我。 –

回答

0

使用fopen功能

if (@fopen($resultUF['destination'], "r")) { 
    echo "File Exist"; 
} else { 
    echo "File Not exist"; 
} 
0

問題是圖像可能不存在或者你沒有直接權限用於訪問圖像,否則你必須指向一個無效位置的形象。

2
$url = 'http://www.example.com/example.jpg)'; 
print_r(get_headers($url)); 

它會給出一個數組。現在你可以檢查響應,看看圖像是否存在

0

你可以使用file_get_contents。這會導致php在返回false的一側發出警告。您可能需要處理此類警告顯示,以確保用戶界面不會與其混淆。

if (file_get_contents($url) === false) { 
     //image not foud 
    } 
+0

如果圖像是10MB原始照片... –

1

您需要檢查該文件是經常存在於服務器或not.you應使用:

is_file。例如

$ URL =「HTTP://www.example。 COM/example.jpg「;

if(is_file($url)) 
{ 
echo "file exists on server"; 
} 
else 
{ 
echo "file not exists on server "; 
} 
0

最快&高效的解決方案損壞或沒有找到圖片鏈接
我建議你不使用和getimagesize(),因爲它第一次將圖像下載然後它會檢查圖像大小+如果這不會形象,那麼它會拋出異常,所以使用下面的代碼

if(checkRemoteFile($imgurl)) 
{ 
//found url, its mean 
echo "this is image"; 
} 

function checkRemoteFile($url) 
{ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$url); 
    // don't download content 
    curl_setopt($ch, CURLOPT_NOBODY, 1); 
    curl_setopt($ch, CURLOPT_FAILONERROR, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    if(curl_exec($ch)!==FALSE) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

注: 這個當前代碼幫助您確定損壞或沒有找到URL圖像這不會幫助你識別圖像類型或標題

相關問題