2013-05-13 57 views
1

我試圖從PHP內部以編程方式下載圖像文件,然後在本地處理它。強大的PHP下載功能?

編輯:前面的功能被下面建議的替換。

我有這樣的功能:

function downloadFile ($url, $path) { 
    $result = false; 
    $userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'; 

    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 

    curl_setopt($ch, CURLOPT_NOBODY, true); 
    curl_setopt($ch, CURLOPT_HEADER, true); 
    curl_exec($ch) ; 
    if(!curl_errno($ch)) 
    { 
     $type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); 
     if (stripos($type, 'image') !== FALSE) 
     { 
      // probably image 

      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      curl_setopt($ch, CURLOPT_NOBODY, false); 
      curl_setopt($ch, CURLOPT_HEADER, false);  
      $fp=fopen($path,'wb'); 
      curl_setopt($ch, CURLOPT_FILE, $fp); 
      curl_exec($ch); 
      fclose($fp); 
      if (exif_imagetype($path) != FALSE) 
      { 
       // 100% image 
       $result = true; 
      } 
      else 
      { 
       // not an image 
       unlink($path); 
      } 
     } 
    } 

    curl_close($ch); 
    return $result; 
} 

我真正需要的是一個函數,它是強大的,可以處理任何類型的圖像,也如果URL是無效的,沒有圖像。

更新:

我改變了我的downloadFile函數與下面的一個建議。在我的本地計算機上它工作的很好,但在我的服務器上失敗:/我有一些文件下載0字節。

UPDATE2:

仍然沒有進展,在服務器由於某種原因,文件沒有下載。除了捲曲之外,是否還有其他要求在服務器上運行? 我也得到一個「2006 - MySQL服務器已經消失」,我相信這是由下載問題引起的。

+0

你爲什麼用'http'替換'https'? – Dogbert 2013-05-13 05:53:45

+0

我試圖去解決https url不工作的問題,但它沒有解決問題。 – Emerson 2013-05-13 05:54:44

+0

直接獲取'https'時會出現什麼錯誤? – Dogbert 2013-05-13 05:55:15

回答

0

最後,這是效果最好的下載功能:

/* 
* This function downloads the image to the plugin/topposts/temp folder 
*/ 
function downloadFile ($url, $path) { 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$con = curl_exec($ch); 
curl_close($ch); 
file_put_contents($path, $con); 
return true; 
} 

不檢查的類型,但工程。謝謝您的幫助。

-1

我認爲你需要安裝openssl並使用PHP進行配置。

1

使用cURL

此功能還檢查圖像的網址。返回true/false(圖像與否)。

function downloadFile ($url, $path) { 
    $result = false; 
    $userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'; 

    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 

    curl_setopt($ch, CURLOPT_NOBODY, true); 
    curl_setopt($ch, CURLOPT_HEADER, true); 
    curl_exec($ch) ; 
    if(!curl_errno($ch)) 
    { 
     $type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); 
     if (stripos($type, 'image') !== FALSE) 
     { 
      // probably image 

      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      curl_setopt($ch, CURLOPT_NOBODY, false); 
      curl_setopt($ch, CURLOPT_HEADER, false);  
      $fp=fopen($path,'wb'); 
      curl_setopt($ch, CURLOPT_FILE, $fp); 
      curl_exec($ch); 
      fclose($fp); 
      if (exif_imagetype($path) != FALSE) 
      { 
       // 100% image 
       $result = true; 
      } 
      else 
      { 
       // not an image 
       unlink($path); 
      } 
     } 
    } 

    curl_close($ch); 
    return $result; 
} 

請求:

if (!downloadFile($url, $path)) 
{ 
    // an error 
} 
+0

謝謝Andrey,但由於某種原因,我無法激活捲曲擴展功能,並且無法在本地進行測試: – Emerson 2013-05-13 06:43:56

+0

什麼原因?檢查libeay32 .dll和ssleay32.dll存在。啓用php_openssl模塊也是。 – 2013-05-13 06:48:30

+0

是的,它們都存在。在apache \ bin和php5.3.13文件夾中。 – Emerson 2013-05-13 06:50:29