2009-11-26 70 views

回答

4
$fp = fopen('logo.png', 'w'); 
fwrite($fp, file_get_contents('http://sstatic.net/so/img/logo.png')); 
fclose($fp); 
+0

所以$計劃生育=的fopen( './ DIR/file.ext','W 「); ???乾杯! – 2011-01-31 06:41:16

3

我就平原file_get_contentsfile_put_contents會做

$content = file_get_contents('http://sstatic.net/so/img/logo.png') 
file_put_contents('logo.png', $content); 

已經被指出的是,在做整個文件的這種方式將在內存中放養,所以你必須要小心memory_limit。如果你需要一個方法,而不是把文件放在內存中,那麼curl就可以做到這一點。

+0

如何用捲曲來做到這一點? – Mask 2009-11-26 09:47:08

0

您可以使用一個捲曲的請求:

public static function curlGet($url) 
{ 
    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $content = curl_exec($ch); 
    curl_close($ch); 
    return $content; 
} 

和寫的內容響應到一個文件

$fp = fopen('logo.png', 'w'); 
fwrite($fp, curlGet('http://sstatic.net/so/img/logo.png')); 
fclose($fp);