From 59987bcfc3ffcfd38f7202a273faabf2a97afa26 Mon Sep 17 00:00:00 2001 From: Chaim Date: Thu, 23 Apr 2020 12:36:18 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E9=97=A8=E9=9D=A2=20-=20=E6=96=87=E4=BB=B6=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/facade/Files.php | 2 ++ src/facade/Returns.php | 43 ++++++++++++++++++++++++++++++++++ src/helper/Files.php | 52 +++++++++++++++++++++++++++++++++++++++++ src/helper/Returns.php | 53 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 src/facade/Returns.php create mode 100644 src/helper/Returns.php diff --git a/src/facade/Files.php b/src/facade/Files.php index 7d62eff..2884f46 100644 --- a/src/facade/Files.php +++ b/src/facade/Files.php @@ -29,6 +29,8 @@ use think\Facade; * @method helper delete(string $name) bool 删除文件 * @method helper deletes(string $name) bool 删除文件夹 * @method helper folderZip(string $name, string $suffix_name = '.png', string $file_name = '*') bool 把文件夹里面的文件打包成zip文件 + * @method helper getFiles(string $path) array|string 获取目录下的所有文件和目录 + * @method helper rmFiles(string $path) bool 删除目录下的文件 */ class Files extends Facade { diff --git a/src/facade/Returns.php b/src/facade/Returns.php new file mode 100644 index 0000000..4bc3341 --- /dev/null +++ b/src/facade/Returns.php @@ -0,0 +1,43 @@ +read())) { + if ($entry !== '.' && $entry !== '..') { + $cur = $path . $entry; + if (is_dir($cur)) { + $subPath = $cur . '/'; + $this->getFiles($subPath); + } + $files[] = $cur; + } + } + $file->close(); + return $files; + } else { + return []; + } + } + + /** + * 删除目录下的文件 + * @param string $path + * @return bool + */ + public function rmFiles(string $path) + { + $files = $this->getFiles($path); + if (!is_array($files)) { + return false; + } elseif (empty($files)) { + return false; + } else { + foreach ($files as $item => $file) { + if (is_dir($file)) { + rmdir($file); + } elseif (is_file($file)) { + unlink($file); + } + } + } + return true; + } } diff --git a/src/helper/Returns.php b/src/helper/Returns.php new file mode 100644 index 0000000..066b31e --- /dev/null +++ b/src/helper/Returns.php @@ -0,0 +1,53 @@ + $code, 'msg' => $msg, 'time' => time(), 'data' => $data])); + } + + /** + * 返回Json-错误 + * @param string $msg 描述 + * @param int $code 状态码 + * @param array $data 数据 + */ + public function jsonError(string $msg = 'error', int $code = 1, array $data = []) + { + date_default_timezone_set('Asia/Shanghai'); + header('Content-Type:application/json; charset=utf-8'); + throw new HttpResponseException(json(['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => $data])); + } +}