%s
';
protected $chunkSize = 100000;
public function __construct($server, $root = '/', $port = 80)
{
$this->root = $root;
parent::__construct($server, $port);
$this->setHeader('User-Agent', 'Rest-Test-Client-Upload 1.0.0');
$this->setHeader('Accept-Language', 'de-de');
//$this->setHeader('Accept-Encoding', 'gzip');
//$this->setHeader('X-Debug', '1');
}
public function setAuth($nickname, $password)
{
$this->setHeader('Authorization', 'Basic ' . base64_encode($nickname . ':' . $password));
}
public function uploadFile($file, $des = 0)
{
$id = $this->initUpload($file);
if (!$id) {
return null;
}
$this->uploadChunks($id, $file);
return $this->finishUpload($id, $file, $des);
}
protected function initUpload($file)
{
$size = filesize($file);
$md5 = md5_file($file);
$cnt = sprintf('%s%s', $size, $md5);
$body = sprintf($this->body, $cnt);
$this->put($this->root . 'explorer/upload', $body);
$res = $this->getResponseBody();
if (preg_match('|(\d+)|i', $res, $match)) {
return $match[1];
}
return null;
}
protected function uploadChunks($id, $file)
{
$chunk = '';
$pos = 0;
$fh = fopen($file, 'r');
while (!feof($fh)) {
$chunk .= fread($fh, 8192);
if (feof($fh) || strlen($chunk) >= $this->chunkSize) {
$chunk = base64_encode($chunk);
$cnt = <<$pos
$chunk
EOT;
$chunk = '';
$pos = ftell($fh);
$body = sprintf($this->body, $cnt);
echo ".";
flush();
$this->post($this->root . 'explorer/upload/' . $id, $body);
$res = $this->getResponseBody();
}
}
fclose($fh);
}
protected function finishUpload($id, $file, $dir)
{
$name = basename($file);
$cnt = <<$name
$dir
EOT;
$body = sprintf($this->body, $cnt);
$this->put($this->root . 'explorer/file/' . $id, $body);
$res = $this->getResponseBody();
if (preg_match('|(\d+)|i', $res, $match)) {
return $match[1];
}
echo "$res\n";
return null;
}
}
?>