diff --git a/controllers/HomeController.php b/controllers/HomeController.php index 1dba1f8..0cd40ac 100644 --- a/controllers/HomeController.php +++ b/controllers/HomeController.php @@ -210,36 +210,42 @@ class HomeController extends Controller */ public function actionDelete() { - $relativePath = Yii::$app->request->post('relativePath'); - $relativePath = rawurldecode($relativePath); - if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) { - throw new NotFoundHttpException('Invalid file path.'); - } - $absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath; - if (!file_exists($absolutePath)) { - throw new NotFoundHttpException('File or directory not found.'); - } else { - $realPath = realpath($absolutePath); - $expectedPathPrefix = realpath(Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id); - if (!str_starts_with($realPath, $expectedPathPrefix)) { - throw new NotFoundHttpException('File or directory not found.'); - } + $relativePaths = Yii::$app->request->post('relativePath'); + if (!is_array($relativePaths)) { + $relativePaths = [$relativePaths]; } - if (is_dir($absolutePath)) { - if (!$this->deleteDirectory($absolutePath)) { - Yii::$app->session->setFlash('error', 'Failed to delete directory.'); - } else { - Yii::$app->session->setFlash('success', 'Directory deleted successfully.'); + foreach ($relativePaths as $relativePath) { + $relativePath = rawurldecode($relativePath); + if (!preg_match($this->pattern, $relativePath) || str_contains($relativePath, '..')) { + throw new NotFoundHttpException('Invalid file path.'); } - } else { - if (!unlink($absolutePath)) { - Yii::$app->session->setFlash('error', 'Failed to delete file.'); + $absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath; + if (!file_exists($absolutePath)) { + throw new NotFoundHttpException('File or directory not found.'); } else { - Yii::$app->session->setFlash('success', 'File deleted successfully.'); + $realPath = realpath($absolutePath); + $expectedPathPrefix = realpath(Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id); + if (!str_starts_with($realPath, $expectedPathPrefix)) { + throw new NotFoundHttpException('File or directory not found.'); + } + } + + if (is_dir($absolutePath)) { + if (!$this->deleteDirectory($absolutePath)) { + Yii::$app->session->setFlash('error', 'Failed to delete directory.'); + } else { + Yii::$app->session->setFlash('success', 'Directory deleted successfully.'); + } + } else { + if (!unlink($absolutePath)) { + Yii::$app->session->setFlash('error', 'Failed to delete file.'); + } else { + Yii::$app->session->setFlash('success', 'File deleted successfully.'); + } } } - return $this->redirect(['index', 'directory' => dirname($relativePath)]); + return $this->redirect(['index', 'directory' => dirname($relativePaths[0])]); } /** diff --git a/web/js/home_script.js b/web/js/home_script.js index f93d052..efc0a77 100644 --- a/web/js/home_script.js +++ b/web/js/home_script.js @@ -150,8 +150,23 @@ $(document).on('click', '.single-share-btn', function () { }); $(document).on('click', '.batch-delete-btn', function () { - console.log('删除按钮被点击'); - // 在这里添加你的代码 + var relativePaths = $('.select-item:checked').map(function () { + return $(this).data('relativePath'); + }).get(); + $.ajax({ + type: "POST", + url: "index.php?r=home%2Fdelete", + data: { relativePath: relativePaths }, + success: function(response) { + // 处理响应 + location.reload(); + }, + error: function() { + // 处理错误 + console.error('AJAX request failed.'); + location.reload(); + } + }); }); //下面的代码实现了各种按钮/样式功能,建议别看了(