PHP實現讀取遠程檔案功能

來源:互聯網
上載者:User

 

在昨天做連接埠測試的基礎上研究一下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 <= 0;
53                 }
54               }
55             }
56             @fclose($fp);
57             return $return;
58           }
59         }


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.