2009-11-26 104 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);