From dd83a6255e95e0a2efd21a975949e3ce1800718e Mon Sep 17 00:00:00 2001 From: Karson Date: Sat, 22 Aug 2020 23:26:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=86=E7=89=87=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=9C=A8Windows=E4=B8=8B=E7=9A=84=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20=E4=BC=98=E5=8C=96=E5=88=86=E7=89=87?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=90=88=E5=B9=B6=E5=A4=B1=E8=B4=A5=E6=97=B6?= =?UTF-8?q?=E6=B8=85=E9=99=A4=E5=88=86=E7=89=87=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/common/library/Upload.php | 39 ++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/application/common/library/Upload.php b/application/common/library/Upload.php index e471007f..c9836d49 100644 --- a/application/common/library/Upload.php +++ b/application/common/library/Upload.php @@ -28,6 +28,8 @@ class Upload */ protected static $maxCheckNums = 10; + protected $merging = false; + protected $chunkDir = null; protected $config = []; @@ -171,7 +173,11 @@ class Upload { $iterator = new \GlobIterator($this->chunkDir . DS . $chunkid . '-*', FilesystemIterator::KEY_AS_FILENAME); $array = iterator_to_array($iterator); - var_dump($array); + foreach ($array as $index => &$item) { + $sourceFile = $item->getRealPath() ?: $item->getPathname(); + $item = null; + @unlink($sourceFile); + } } /** @@ -195,6 +201,7 @@ class Upload } } if (!$completed) { + $this->clean($chunkid); throw new UploadException(__('Chunk file info error')); } @@ -202,6 +209,7 @@ class Upload $uploadPath = $filePath; if (!$destFile = @fopen($uploadPath, "wb")) { + $this->clean($chunkid); throw new UploadException(__('Chunk file merge error')); } if (flock($destFile, LOCK_EX)) { // 进行排他型锁定 @@ -229,12 +237,15 @@ class Upload 'error' => 0, 'size' => $file->getSize() ]; - $file->setUploadInfo($info); + $file->setSaveName($filename)->setUploadInfo($info); $file->isTest(true); //重新设置文件 $this->setFile($file); + unset($file); + $this->merging = true; + //允许大文件 $this->config['maxsize'] = "1024G"; @@ -287,14 +298,28 @@ class Upload $uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1); $fileName = substr($savekey, strripos($savekey, '/') + 1); - $destDir = ROOT_PATH . 'public' . $uploadDir; + $destDir = ROOT_PATH . 'public' . str_replace('/', DS, $uploadDir); $sha1 = $this->file->hash(); - $file = $this->file->move($destDir, $fileName); - if (!$file) { - // 上传失败获取错误信息 - throw new UploadException($this->file->getError()); + //如果是合并文件 + if ($this->merging) { + if (!$this->file->check()) { + throw new UploadException($this->file->getError()); + } + $destFile = $destDir . $fileName; + $sourceFile = $this->file->getRealPath() ?: $this->file->getPathname(); + $info = $this->file->getInfo(); + $this->file = null; + rename($sourceFile, $destFile); + $file = new File($destFile); + $file->setSaveName($fileName)->setUploadInfo($info); + } else { + $file = $this->file->move($destDir, $fileName); + if (!$file) { + // 上传失败获取错误信息 + throw new UploadException($this->file->getError()); + } } $this->file = $file; $params = array(