2017-09-04 97 views
1

所以我想,以檢查是否有來自get_the_post_thumbnail_url()PHP檢查,如果文件上WEBP圖片不能正常工作

檢索這不是工作,我怎麼會想到,雖然在URL中的WEBP圖片格式存在。

if (!file_exists($thePostThumbUrl)) 
    $thePostThumbUrl = str_replace("_result.webp", "." . $ext, $thePostThumbUrl); 

如果我贊同拇指URL它得到正確的圖像與.WEBP格式

echo $thePostThumbUrl . '<br/ >'; 

顯示:

圖像URL + _result.webp

這裏是工作的代碼IM

我知道PHP im的工作版本是PHP/5.6.30

回答

1

確定,所以作爲Akintunde建議的,file_exists功能不會與圖像的URL工作。所以需要修改代碼來使用服務器路徑。

此代碼的伎倆:

$ext = pathinfo($thePostThumbUrl, PATHINFO_EXTENSION); 
$thePostThumbPath = str_replace("http://localhost", "", $thePostThumbUrl); 
if (!file_exists($_SERVER['DOCUMENT_ROOT'] . $thePostThumbPath)) { 
    $thePostThumbUrl = str_replace("_result.webp", "." . $ext, $thePostThumbUrl); 
} 

Thansk Akintunde指着我在正確的方向:)

0

您需要在這種情況下使用CURL,因爲它是一個URL。

實施例:

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; 
    } 
} 
+0

這並沒有爲我工作,但不會給它一個downvote因爲也許我沒有正確實施。請參閱我的答案,瞭解我如何解決問題。謝謝胡安:) –