From 468b37b85af2f0f009a9391bfd0fe97e28a41acb Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Tue, 13 Feb 2024 20:44:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=8A=9F=E8=83=BD=20=E4=B8=BA=E4=BA=86=E4=B8=AA?= =?UTF-8?q?=E8=BF=9B=E5=BA=A6=E6=9D=A1=E6=95=B4=E4=BA=86=E4=B8=80=E5=A0=86?= =?UTF-8?q?=E6=B4=BB=20=E7=9B=AE=E5=89=8D=E5=8A=9F=E8=83=BD=E8=BF=98?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E5=AE=8C=E5=96=84(=E5=8C=85=E6=8B=AC?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E9=87=8D=E5=A4=8D=E7=9A=84=E6=A3=80=E6=B5=8B?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/HomeController.php | 39 +++++++++++++++++++++++++++ models/UploadForm.php | 48 ++++++++++++++++++++++++++++++++++ views/home/index.php | 18 ++++++++++++- web/css/home_style.css | 3 ++- web/js/home_script.js | 34 +++++++++++++++++++++--- 5 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 models/UploadForm.php diff --git a/controllers/HomeController.php b/controllers/HomeController.php index ada0e1c..42d23a5 100644 --- a/controllers/HomeController.php +++ b/controllers/HomeController.php @@ -3,6 +3,7 @@ namespace app\controllers; use app\models\RenameForm; +use app\models\UploadForm; use app\utils\FileTypeDetector; use Yii; use yii\filters\VerbFilter; @@ -10,10 +11,12 @@ use yii\helpers\ArrayHelper; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\web\Response; +use yii\web\UploadedFile; class HomeController extends Controller { protected string $pattern = '/^[^\p{C}\/:*?"<>|\\\\]+$/u'; + public function behaviors() { return array_merge( @@ -26,6 +29,7 @@ class HomeController extends Controller 'download' => ['GET'], 'rename' => ['POST'], 'delete' => ['POST'], + 'upload' => ['POST'], ], ], ] @@ -254,4 +258,39 @@ class HomeController extends Controller } return true; } + + /** + * 文件上传 + * https://devs.chenx221.cyou:8081/index.php?r=home%2Fupload + * + * @return string|Response + */ + public function actionUpload() + { + $model = new UploadForm(); + $model->targetDir = Yii::$app->request->post('targetDir', '.'); + $uploadedFiles = UploadedFile::getInstancesByName('files'); + $successCount = 0; + $totalCount = count($uploadedFiles); + + foreach ($uploadedFiles as $uploadedFile) { + $model->uploadFile = $uploadedFile; + if (!preg_match($this->pattern, $model->uploadFile->baseName)) { + continue; + } + if ($model->upload()) { + $successCount++; + } + } + + if ($successCount === $totalCount) { + Yii::$app->session->setFlash('success', 'All files uploaded successfully.'); + } elseif ($successCount > 0) { + Yii::$app->session->setFlash('warning', 'Some files uploaded successfully.'); + } else { + Yii::$app->session->setFlash('error', 'Failed to upload files.'); + } + //返回状态码200 + return Yii::$app->response->statusCode = 200; // 如果出错请删掉return + } } diff --git a/models/UploadForm.php b/models/UploadForm.php new file mode 100644 index 0000000..88e4c19 --- /dev/null +++ b/models/UploadForm.php @@ -0,0 +1,48 @@ + false, 'checkExtensionByMimeType' => false], //这规则奇怪的放走近科学都可以拍好几集了 + ]; + } + + public function upload() + { + if ($this->validate()) { + if ($this->targetDir === null) { + $this->targetDir = '.'; + } + if (str_contains($this->targetDir, '..')) { + return false; + } + $userHomeDir = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id; + $absolutePath = $userHomeDir . '/' . $this->targetDir; + if (!is_dir($absolutePath)) { + return false; + } + $fileName = $this->uploadFile->baseName; + if ($this->uploadFile->extension !== '') { + $fileName .= '.' . $this->uploadFile->extension; + } + $this->uploadFile->saveAs($absolutePath . '/' . $fileName); + return true; + } else { + return false; + } + } +} \ No newline at end of file diff --git a/views/home/index.php b/views/home/index.php index 08cbdec..4710726 100644 --- a/views/home/index.php +++ b/views/home/index.php @@ -7,10 +7,12 @@ /* @var $directory string 当前路径 */ use app\models\RenameForm; +use app\models\UploadForm; use yii\bootstrap5\ActiveForm; use yii\bootstrap5\Html; use app\assets\FontAwesomeAsset; use yii\bootstrap5\Modal; +use yii\bootstrap5\Progress; use yii\helpers\Url; use yii\web\JqueryAsset; use yii\web\View; @@ -23,7 +25,7 @@ JqueryAsset::register($this); $this->registerCssFile('@web/css/home_style.css'); ?>
-
+

title) ?>

+ + 0, + 'barOptions' => ['class' => ['bg-success', 'progress-bar-animated', 'progress-bar-striped']], + 'label' => '123', //NMD 不是说可选吗 + 'options' => ['style' => 'display: none;margin-top: 10px;', 'id' => 'progress-bar'] + ]); + ?> +