On the basis of the port test yesterday to study the PHP upload and download code, the results reminded of the time before the written question is in the upload file to display the contents of the file, so I read the PHP implementation of remote file is very interested in the function, the following is code:
| 01 |
function Urlfopen ($url, $limit = 0, $post = ', $cookie = ', $bysocket = FALSE, $ip = ', $timeout =, $block = TRUE, $encodetype = ' UrlEncode ') { |
| 03 |
$matches = Parse_url ($url); |
| 04 |
$host = $matches [' Host ']; |
| 05 |
$path = $matches [' Path ']? $matches [' Path ']. (Isset ($matches [' query '])? '?'. $matches [' query ']: ': '/'; |
| 06 |
$port =!empty ($matches [' Port '])? $matches [' Port ']: 80; |
| 09 |
$out = "POST $path http/1.0\r\n"; |
| 10 |
$out. = "Accept: */*\r\n"; |
| 11 |
$out. = "accept-language:zh-cn\r\n"; |
| 12 |
$boundary = $encodetype = = ' UrlEncode '? '' : ';'. substr ($post, 0, Trim (strpos ($post, "\ n")); |
| 13 |
$out. = $encodetype = = ' UrlEncode '? "content-type:application/x-www-form-urlencoded\r\n": "content-type:multipart/form-data$boundary\r\n"; |
| 14 |
$out. = "User-agent: $_server[http_user_agent]\r\n"; |
| 15 |
$out. = "Host: $host \ r \ n"; |
| 16 |
$out. = ' content-length: '. strlen ($post). " \ r \ n "; |
| 17 |
$out. = "connection:close\r\n"; |
| 18 |
$out. = "cache-control:no-cache\r\n"; |
| 19 |
$out. = "Cookie: $cookie \r\n\r\n"; |
| 22 |
$out = "Get $path http/1.0\r\n"; |
| 23 |
$out. = "Accept: */*\r\n"; |
| 24 |
$out. = "accept-language:zh-cn\r\n"; |
| 25 |
$out. = "User-agent: $_server[http_user_agent]\r\n"; |
| 26 |
$out. = "Host: $host \ r \ n"; |
| 27 |
$out. = "Referer: \ r \ n"; |
| 28 |
$out. = "connection:close\r\n"; |
| 29 |
$out. = "Cookie: $cookie \r\n\r\n"; |
| 31 |
$fp = @fsockopen ($ip, $ip: $host), $port, $errno, $errstr, $timeout); |
| 35 |
Stream_set_blocking ($fp, $block); |
| 36 |
Stream_set_timeout ($fp, $timeout); |
| 38 |
$status = Stream_get_meta_data ($FP); |
| 39 |
if (! $status [' timed_out ']) { |
| 41 |
if ($header = @fgets ($fp)) && ($header = = "\ r \ n" $header = = "\ n")) { |
| 47 |
while (!feof ($FP) &&! $stop) { |
| 48 |
$data = Fread ($fp, ($limit = 0 $limit > 8192 8192: $limit)); |
| 51 |
$limit-= strlen ($data); |