您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP实现读取远程文件功能

2025/8/20 17:53:25发布20次查看
在昨天做端口测试的基础上研究一下php上传与下载的代码,结果想起前段时间笔试题有一道题是在上传文件时显示文件内容,让我对php实现读取远程文件的功能很感兴趣,以下是代码:
01 function urlfopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = false  , $ip = '', $timeout = 15, $block = true, $encodetype  = 'urlencode') {
02           $return = '';
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;
07
08           if($post) {
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;
20             $out .= $post;
21           } else {
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;
30           }
31           $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr,$timeout);
32           if(!$fp) {
33             return '';
34           } else {
35             stream_set_blocking($fp, $block);
36             stream_set_timeout($fp, $timeout);
37             @fwrite($fp, $out);
38             $status = stream_get_meta_data($fp);
39             if(!$status['timed_out']) {
40               while (!feof($fp)) {
41                 if(($header = @fgets($fp)) && ($header == \r\n   $header ==\n)) {
42                   break;
43                 }
44               }
45
46               $stop = false;
47               while(!feof($fp) && !$stop) {
48                 $data = fread($fp, ($limit == 0  $limit > 8192 ? 8192 :$limit));
49                 $return .= $data;
50                 if($limit) {
51                   $limit -= strlen($data);
52                   $stop = $limit
53                 }
54               }
55             }
56             @fclose($fp);
57             return $return;
58           }
59         }
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product