2013-08-18 23 views
2

我有http://www.reelfilmlocations.co.ukPHP檢查文件是否在外部杜曼存在(訪問形成子域)

一個網站上面的網站有一個上傳的子文件夾中創建在圖像被上傳一個管理區域和不同大小的副本/ images目錄。

我爲移動設備創建一個網站,這將在一個子域操作,但使用的數據庫和圖像來自主域名, http://2012.reelfilmlocations.co.uk

我希望能夠訪問那些圖像在父域,我可以通過鏈接到完整域的圖像,即http:www.reelfilmlocations.co.uk/images/minidisplay/myimage.jpg

雖然我需要檢查圖像是否存在第一...

我有一個PHP函數來檢查,如果圖像存在,如果是這樣我t返回圖像的完整url。

如果它不存在,我想回到一個圖像佔位符的路徑。

以下函數返回正確的圖像(如果存在),但如果沒有,則返回路徑到佔位符圖像所在的目錄,即http://www.reelfilmlocations.co.uk/images/thumbs/。沒有no-image.jpg位。

有問題的頁面是:http://2012.reelfilmlocations.co.uk/browse-unitbases/ 代碼我有我的頁面獲得的圖像是:

<img src="<?php checkImageExists('/uploads/images/thumbs/', $row_rs_locations['image_ubs']);?>"> 

我的PHP函數:

if(!function_exists("checkImageExists")){ 
    function checkImageExists($path, $file){ 
     $imageName = "http://www.reelfilmlocations.co.uk".$path.$file; 
     $header_response = get_headers($imageName, 1); 
     if(strpos($header_response[0], "404") !== false){ 
      // NO FILE EXISTS 
      $imageName = "http://www.reelfilmlocations.co.uk".$path."no-image.jpg"; 
     }else{ 
      // FILE EXISTS!! 
      $imageName = "http://www.reelfilmlocations.co.uk".$path.$file; 
     } 
     echo($imageName); 
    } 
} 

未能得到這個工作,我做了一些周圍挖和閱讀有關捲曲一些帖子:

這只是返回一個佔位符圖像每次。

if(!function_exists("remoteFileExists")){ 
    function remoteFileExists($url) { 
     $curl = curl_init($url); 

     //don't fetch the actual page, you only want to check the connection is ok 
     curl_setopt($curl, CURLOPT_NOBODY, true); 

     //do request 
     $result = curl_exec($curl); 

     $ret = false; 

     //if request did not fail 
     if ($result !== false) { 
      //if request was ok, check response code 
      $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
      if ($statusCode == 200) { 
       $ret = true; 
      } 
     } 
     curl_close($curl); 

     return $ret; 
    } 
} 

if(!function_exists("checkImageExists")){ 
    function checkImageExists($path, $file){ 
     $imageName = "http://www.reelfilmlocations.co.uk".$path.$file; 

     $exists = remoteFileExists($imageName); 
     if ($exists){ 
      // file exists do nothing we already have the correct $imageName 
     } else { 
        // file does not exist so set our image to the placeholder 
      $imageName = "http://www.reelfilmlocations.co.uk".$path."no-image.jpg"; 
     } 
      echo($imageName); 
    } 
} 

我不知道它是否可以得到403,或如何檢查是否是這種情況。

我可以嘗試任何指針或東西woud不勝感激。

回答

1

我使用它CURL,發出HEAD請求並檢查響應代碼做。

沒有測試,但應該做的伎倆:

$URL = 'sub.domain.com/image.jpg'; 
$res = `curl -s -o /dev/null -IL -w "%{http_code}" http://$URL`; 
if ($res == '200') 
    echo 'Image exists'; 

上面的代碼將填充$res與徵用的狀態代碼(注意,我不包括http://前綴,因爲我的$URL變量在命令行中執行它

當然,使用PHP的CURL函數也可以獲得相同的結果,並且上面的調用可能無法在您的服務器上運行,我只是解釋如果我有相同的需求。

+0

謝謝你的回覆,我認爲最主要的問題是我有子域作爲物理目錄而不是作爲父域內的文件夾,並且我得到403錯誤,因爲子域沒有權限訪問主域名,我可能會抓住吸管,我已經創建了一個新的子域作爲主域上的文件夾,並會看到是否有助於... –

+1

如果圖像預計在腳本的相同文件系統中,可以簡單地使用:'file_exists()'。 :) –

+0

Janio:事實上,這就是我在主域上使用的東西,它工作的一種享受,雖然因爲它的子域我不能使用文件存在作爲其外部網址到我所在的子域。 .. –

相關問題