note 12991 deleted from function.fsockopen by jimw
| From: | jimw@php.net | Date: | Sat, 08 Dec 2001 21:56:56 +0000 |
| Subject: | note 12991 deleted from function.fsockopen by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22253@lists.php.net to get a copy of this message | ||
I use the function "fsockopen" to get the files from the other servers
the code:
-------------------------------
Main Code:
<?
ob_implicit_flush();
for($j=0;$j<500;$j++)print(" ");
echo "\n";
if($HTTP_POST_VARS[submit]) {
if($HTTP_POST_VARS[saveas]=='' || $HTTP_POST_VARS[url]=='') {
die('error');
}
set_time_limit(0);
require('zlib_getfile.inc.php');
$s = new getFile($url, $saveas);
echo "FileSize£º".$s->size()."<br>";
echo "FileType£º".$s->type()."<br>";
chmod($HTTP_POST_VARS[saveas], 0777);
echo "Finished£¬<a href=$saveas>Click here to download!</a>";
flush();
} else {
?>
<form method=post>
FileURL£º<input type=text name=url>
SaveAs Name£º<input type=text name=saveas>
<input type=submit name=submit value="Send!">
</form>
<?
}
?>
class file zlib_getfile.inc.php:
<?
// 2001,5,20 by y10k
Class getFile{
var $info = array();
var $size = 0;
var $type = 'unknown';
var $lastModified = '';
var $sp;
var $content;
var $header;
var $max_run_time = 30;
var $max_get_size = 1024;
var $max_ct_size = 4098;
var $error_no;
var $error_str;
var $sp;
var $requestheader = '';
var $request_var = 'HTTP/1.1';
var $fp;
var $saveas;
function getFile($url = '', $saveas = '', $request_ver =
'HTTP/1.1') {
$this->request_ver = $request_ver;
if($url) {
$this->parse($url);
$this->reset();
if($saveas) {
$this->saveas = $saveas;
if(file_exists($this->saveas)) {
$filesize = filesize($this->saveas);
$this->requestheader .= "\r\nRange:
bytes=".$filesize."-";
$this->fp = fopen($this->saveas, 'a');
} else {
$this->fp = fopen($this->saveas, 'w');
}
}
$this->connect();
$this->getHeader();
if($saveas) {
$this->save();
} else {
$this->get();
}
}
}
function parse($url, $request_ver = 'HTTP/1.1') {
$this->info[url] = $url;
$tmp = parse_url($url);
$this->info[port] = ($tmp[port]) ? $tmp[port] : 80;
$this->info[query] = ($tmp[query]) ? '?'.$tmp[query] : '' ;
if(!$tmp[path]) {
return 0;
}
if(!$tmp[host]) {
return 0;
}
$this->info[host] = $tmp[host];
$this->info[path] = $tmp[path];
}
function reset() {
$this->max_ct_size = ($this->max_ct_size) ? $this->max_ct_size : 1024;
$this->max_get_size = ($this->max_get_size) ? $this->max_get_size : 1024;
$this->max_run_time = ($this->max_run_time) ? $this->max_run_time : 30;
$request = 'GET '.$this->info[path].$this->info[query].'
'.$this->request_ver;
$request.= "\r\nHost: ".$this->info[host];
$request.= "\r\nAccept:*/*";
$this->requestheader = $request;
}
function connect() {
$this->sp = fsockopen($this->info[host], $this->info[port],
&$this->error_no, &$this->error_str, $this->max_run_time);
if(!$this->sp) {
return 0;
}
$this->requestheader .= "\r\n\r\n";
fputs($this->sp, $this->requestheader);
}
function getHeader() {
$i=0;
$buffer = "\n";
while($buffer) {
$i++;
$buffer = fgets($this->sp, $this->max_get_size);
$this->header .= $buffer;
$buffer = trim($buffer);
}
preg_match("/Content-Type: (.+)/i", $this->header, $tmp);
$this->type = ($tmp[1]) ? $tmp[1] : 'unknown';
preg_match("/Content-Length: (\d+)/i", $this->header, $tmp);
$this->size = ($tmp[1]) ? $tmp[1] : 'unknown';
}
function get() {
while(!feof($sp)) {
$buffer = fread($this->sp, $this->max_get_size);
$content .= $buffer;
}
$this->content = $content;
}
function save() {
$i = 0;
while($buffer || !$i) {
$i++;
$buffer = fread($this->sp, $this->max_get_size);
$content .= $buffer;
if(strlen($content) > $this->max_ct_size) {
fwrite($this->fp, $content);
$content = '';
}
}
fwrite($this->fp, $content);
fclose($this->fp);
}
function size() {
return $this->size;
}
function type() {
return $this->type;
}
function lastModified() {
return $this->lastModified;
}
function sp() {
return $this->sp;
}
function content() {
return $this->content;
}
function host() {
return $this->info[host];
}
function path() {
return $this->info[path];
}
function header() {
return $this->header;
}
function export($file) {
$p = fopen($file, 'w');
fwrite($p, $this->content);
fclose($p);
}
}
?>
I had got the Foxserv0.3(18MB) from www.fox-hunt.com to my own server(zphp.com),only 15s
see the highlight code from http://www.zphp.com/bbs/html/123.htm