From a0aca6c6df62ef15a95eb4637327c28a549876de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Thu, 13 May 2021 18:41:39 +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 | 43 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/HttpServer/Http/File.php b/HttpServer/Http/File.php index c9d7eac1..e422fefd 100644 --- a/HttpServer/Http/File.php +++ b/HttpServer/Http/File.php @@ -50,12 +50,16 @@ class File /** * @return string + * @throws Exception */ public function rename(): string { if (!empty($this->newName)) { return $this->newName; } + if (!file_exists($this->getTmpPath())) { + throw new Exception('(' . $this->name . ')Failed to open stream: No such file or directory'); + } $param = ['tmp_name' => $this->getTmpPath()]; $this->newName = Snowflake::rename($param); return $this->newName; @@ -68,33 +72,28 @@ class File */ public function getContent(): string { -// $open = fopen($this->getTmpPath(), 'r'); + $open = fopen($this->getTmpPath(), 'r'); @move_uploaded_file($this->tmp_name, storage($this->name)); + $limit = 1024000; - var_dump(file_get_contents($this->getTmpPath())); + $stat = fstat($open); - return ''; - -// $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; + $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; }