我試圖從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服務器已經消失」,我相信這是由下載問題引起的。
你爲什麼用'http'替換'https'? – Dogbert 2013-05-13 05:53:45
我試圖去解決https url不工作的問題,但它沒有解決問題。 – Emerson 2013-05-13 05:54:44
直接獲取'https'時會出現什麼錯誤? – Dogbert 2013-05-13 05:55:15