<?php $handle = fopen ("/home/rasmus/file.txt", "r"); $handle = fopen ("/home/rasmus/file.gif", "wb"); $handle = fopen ("http://www.example.com/", "r"); $handle = fopen ("ftp://user:[email protected]/somefile.txt", "w"); ?>
<?php $file = fopen ("http://www.example.com/", "r"); if (!$file) { echo "<p>Unable to open remote file.\n"; exit; } while (!feof ($file)) { $line = fgets ($file, 1024); /* This only works if the title and its tags are on one line */ if (eregi ("<title>(.*)</title>", $line, $out)) { $title = $out[1]; break; } } fclose($file); ?>
<?php $file = fopen ("ftp://ftp.example.com/incoming/outputfile", "w"); if (!$file) { echo "<p>Unable to open remote file for writing.\n"; exit; } /* Write the data here. */ fputs ($file, $_SERVER['HTTP_USER_AGENT'] . "\n"); fclose ($file); ?>
<?php $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = "GET / HTTP/1.1\r\n"; $out .= "Host: example.com\r\n"; $out .= "Connection: Close\r\n\r\n"; fputs($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); } ?>
<?php $fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr); if (!$fp) { echo "ERROR: $errno - $errstr<br />\n"; } else { fwrite($fp, "\n"); echo fread($fp, 26); fclose($fp); } ?>
<?php //定义一些连接参数 $urls = array( 'host'=>'localhost', 'port'=>80, 'path'=>'/index.php', 'method'=>'POST', 'protocol'=>'1.0', ); //POST方法传递的参数 $ps = array( 'language'=>'php', 'linux'=>'redhat', ); //GET方法传递的参数 $gs = array( 'php'=>5, 'redhat'=>9 ); /** 返回:得到POST或GET方法后返回的字符串(string) 参数: $usls : string $ps : array $gs : array 调用方法: getData($urls,$ps,'') //使用POST方法 getData($urls,'',$gs) //使用GET方法 参考资料:http://cn.php.net/manual/en/function.fsockopen.php */ function getData($urls,$ps='',$gs=''){ $host = $urls['host']; $port = $urls['port']; $path = $urls['path']; $method = $urls['method']; $protocol = $urls['protocol']; $posts = ''; $gets = ''; if(is_array($ps)){ foreach($ps as $k => $v){ $posts .= urlencode($k)."=".urlencode($v).'&'; } $posts = substr($posts,0,-1); $len = strlen($posts); } if(is_array($gs)){ foreach($gs as $k => $v){ $gets .= urlencode($k)."=".urlencode($v).'&'; } $gets = substr($gets,0,-1); } $fp = fsockopen($host, $port,$errno,$errstr,3); if(!$fp){ echo "can't connect...\r\n<br>Error:$errstr"; return ; } fputs($fp, "$method $path?$gets HTTP/$protocol\r\n"); fputs($fp, "Host: localhost\r\n"); if($posts != ''){ fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); fputs($fp, "Content-Length: $len\r\n"); fputs($fp, "\r\n"); fputs($fp, $posts); } fputs($fp, "Connection: Close\r\n\r\n"); $s = ''; do{ $data = fgets($fp,1024); if($data == '') { break; } $s .= $data; } while(true); fclose($fp); return $s; } //这里是使用POST方法取得目标网页返回的字符串 echo getData($urls,$ps,''); //如果要使用GET方法就用如下方式: echo getData($urls,'',$gs); ?>
<?php $fp=@fopen($url,"r") or die ("cannot open $url"); while($line=@fgets($fp,1024)) { $contents.=$line; } echo $contents; //显示文件内容 fclose($fp); //关闭文件 ?>
<?php $fp = fopen($_GET['filename'], 'rb'); $data = $tmp = ''; while ( true ) { $tmp = fgets($fp, 1024); if ( 0 === strlen($tmp) ) { break; } $data .= $tmp; } fclose($fp); $file=preg_replace("/^.+\//","",$filename); //write $fp = fopen("$file", 'wb'); fwrite($fp, $data); fclose($fp); ?>
<? $url = getenv("QUERY_STRING"); if(!ereg("^http",$url)) { echo "example:<br>xxx.php?http://jsw.china12e.com/<;br>"; exit; } if($url) $url=str_replace("\\","/",$url); $f=@fopen($url,"r"); $a=""; if($f) { while(!feof($f)) $a.=@fread($f,8000); fclose($f); } $rooturl = preg_replace("/(.+\/)(.*)/i","\\1",$url); $a = preg_replace("/(src[[:space:]]*=['\"])([^h].*?)/is","\\1$rooturl\\2",$a); $a = preg_replace("/(src[[:space:]]*=)([^h'\"].*?)/is","\\1$rooturl\\2",$a); $a = preg_replace("/(action[[:space:]]*=['\"])([^h].*?)/is","\\1$php_self?$rooturl\\2",$a); $a = preg_replace("/(action[[:space:]]*=)([^h'\"].*?)/is","\\1$php_self?$rooturl\\2",$a); $a = preg_replace("/(<a.+?href[[:space:]]*=['\"])([^h].*?)/is","\\1$php_self?$rooturl\\2",$a); $a = preg_replace("/(<a.+?href[[:space:]]*=[^'\"])([^h].*?)/is","\\1$php_self?$rooturl\\2",$a); $a = preg_replace("/(link.+?href[[:space:]]*=[^'\"])(.*?)/is","\\1$rooturl\\2",$a); $a = preg_replace("/(link.+?href[[:space:]]*=['\"])(.*?)/is","\\1$rooturl\\2",$a); echo $a; ?>
<?php $url="http://bbs.icehack.com/register.php?step=2&addpassword=aaaaaa&addpassword2=aaaaaa&[email protected]&addusername="; for($i=1131;$i<=1150;$i++) { $urls=$url.$i; $f=@fopen($urls,"r"); $a=@fread($f,10); fclose($f); } ?>
<?php $url="http://bbs.icehack.com/index.php?addusername="; for($i=1131;$i<=1180;$i++) { $urls=$url.$i; $f=@fopen($urls,"r"); $a=@fread($f,10); fclose($f); } ?>
$url = "http://www.electrictoolbox.com/php-extract-domain-from-full-url/"; $parts = parse_url($url); // Array ( [scheme] => http [host] => www.electrictoolbox.com [path] => /php-extract-domain-from-full-url/ )
<?php $url = 'http://username:password@hostname/path?arg=value#anchor'; print_r(parse_url($url)); echo parse_url($url, PHP_URL_PATH); // Array ( [scheme] => http [host] => hostname [user] => username [pass] => password [path] => /path [query] => arg=value [fragment] => anchor )
if ( !function_exists( 'fopen_url' ) ) { function fopen_url($url) { $file_content = ''; if (function_exists('file_get_contents')) $file_content = @file_get_contents($url); elseif (ini_get('allow_url_fopen') && ($file = @fopen($url, 'rb'))) { $i = 0; while (!feof($file) && $i++ < 1000) $file_content .= strtolower(fread($file, 4096)); fclose($file); } elseif (function_exists('curl_init')) { $curl_handle = curl_init(); curl_setopt($curl_handle, CURLOPT_URL, $url); curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,5); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1); curl_setopt($curl_handle, CURLOPT_FAILONERROR,1); curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Trackback Spam Check'); $file_content = curl_exec($curl_handle); curl_close($curl_handle); } return $file_content; } }