Way 1:sockets
Copy CodeThe code is as follows:
$a = "http://jb51.net/content/uploadfile/201106/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg";
$local = ' socket1.gif ';
$AA = getimg ($a, $local);
/*
*@ Full Picture Address
*@ the file name to store
*/
function getimg ($url = "", $filename = "") {
if (Is_dir (basename ($filename))) {
echo "The Dir is not exits";
Return false;
}
Remove the possible quotes above the URL connection
$url = Preg_replace ('/(?: ^[\ ' "]+|[ \ ' "\/]+$)/', ', $url);
if (!extension_loaded (' Sockets ')) return false;
Get information about the 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;
}
Relative address of picture
$msg = ' GET '. $matches [3]. "Http/1.1\r\n";
Host Name
$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;
}
Way 2:curl
Copy CodeThe code is as follows:
$url = "Http://jb51.net/content/uploadfile/201106/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg";
$filename = ' curl.gif ';
Http://jb51.net
Getimg ($url, $filename);
/*
*@ through curl to get the developed picture to the local
*@ Full Picture Address
*@ the file name to store
*/
function getimg ($url = "", $filename = "") {
if (Is_dir (basename ($filename))) {
echo "The Dir is not exits";
Return false;
}
Remove the possible quotes above the URL connection
$url = Preg_replace ('/(?: ^[\ ' "]+|[ \ ' "\/]+$)/', ', $url);
$hander = Curl_init ();
$fp = fopen ($filename, ' WB ');
curl_setopt ($hander, Curlopt_url, $url);
curl_setopt ($hander, Curlopt_file, $fp);
curl_setopt ($hander, curlopt_header,0);
curl_setopt ($hander, curlopt_followlocation,1);
curl_setopt ($hander, curlopt_returntransfer,false);//returns data as a data stream, when false is displayed directly
curl_setopt ($hander, curlopt_timeout,60);
/* $options = Array (
Curlopt_url=> ' Http://jb51.net/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;
}
?>
http://www.bkjia.com/PHPjc/324605.html www.bkjia.com true http://www.bkjia.com/PHPjc/324605.html techarticle the way 1:sockets copy code code is as follows: $a = "http://jb51.net/content/uploadfile/201106/ Thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg "; $local = ' socket1.gif '; $aa = ...