From d60a0fda54afe9f4eb4cf48657a2a495bb9a1458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Thu, 13 May 2021 18:54:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HttpServer/Http/File.php | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/HttpServer/Http/File.php b/HttpServer/Http/File.php index 88e502d7..44c818ba 100644 --- a/HttpServer/Http/File.php +++ b/HttpServer/Http/File.php @@ -44,7 +44,7 @@ class File throw new Exception($this->getErrorInfo()); } - file_put_contents($path, $this->_content); + @move_uploaded_file($this->tmp_name, $path); if (!file_exists($path)) { return false; } @@ -61,12 +61,42 @@ class File return $this->newName; } - $this->_content = file_get_contents($this->getTmpPath()); + if (!file_exists($this->getTmpPath())) { + throw new Exception('(' . $this->name . ')Failed to open stream: No such file or directory'); + } - $this->newName = md5($this->_content); + return $this->name; + } -// $this->newName = Snowflake::rename($this->getTmpPath()); - return $this->newName; + + /** + * @return string + * @throws Exception + */ + public function getContent(): string + { + $open = fopen($this->getTmpPath(), 'r'); + +// @move_uploaded_file($this->tmp_name, storage($this->name)); + + $limit = 1024000; + + $stat = fstat($open); + + $sleep = $offset = 0; + $content = ''; + while ($file = fread($open, $limit)) { + $content .= $file; + fseek($open, $offset); + if ($sleep > 0) { + sleep($sleep); + } + if ($offset >= $stat['size']) { + break; + } + $offset += $limit; + } + return $content; }