方式1:sockets
复制代码 代码如下:
$a = http://bitscn.com/content/uploadfile/201106/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg;
$local = 'socket1.gif';
$aa = getimg($a,$local);
/*
*@ 完整的图片地址
*@ 要存储的文件名
*/
function getimg( $url = , $filename = ) {
if(is_dir(basename($filename))) {
echo the dir was not exits;
return false;
}
//去除url连接上面可能的引号
$url = preg_replace( '/(?:^[\']+|[\'\/]+$)/', '', $url );
if (!extension_loaded('sockets')) return false;
//获取url各相关信息
preg_match( '/http:\/\/([^\/\:]+(\:\d{1,5})?)(.*)/i', $url, $matches );
if (!$matches) return false;
$sock = socket_create( af_inet, sock_stream, sol_tcp );
if ( !@socket_connect( $sock, $matches[1], $matches[2] ? substr($matches[2], 1 ) : 80 ) ) {
return false;
}
//图片的相对地址
$msg = 'get ' . $matches[3] . http/1.1\r\n;
//主机名称
$msg .= 'host: ' . $matches[1] . \r\n;
$msg .= 'connection: close' . \r\n\r\n;
socket_write( $sock, $msg );
$bin = '';
while ( $tmp = socket_read( $sock, 10 ) ) {
$bin .= $tmp;
$tmp = '';
}
$bin = explode(\r\n\r\n, $bin);
$img = $bin[1];
$h = fopen( $filename, 'wb' );
$res = fwrite( $h, $img ) === false ? false : true;
@socket_close( $sock );
return $res;
}
方式2:curl
复制代码 代码如下:
'http://bitscn.com/content/uploadfile/201106/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg',
curlopt_file => $fp,
curlopt_header => 0,
curlopt_followlocation => 1,
curlopt_timeout => 60
);
curl_setopt_array($hander, $options);
*/
curl_exec($hander);
curl_close($hander);
fclose($fp);
return true;
}
?>