Way 1:sockets
Copy Code code as follows:
$a = "yun_qi_img/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 all the relevant 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 Code code as follows:
<?php
$url = "Yun_qi_img/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg";
$filename = ' curl.gif ';
Http://jb51.net
Getimg ($url, $filename);
/*
*@ through the curl way to obtain 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 data flow, 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;
}
?>