From a466b65f7761e97dabdffc2293048222ce032a49 Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Wed, 14 Feb 2024 19:12:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=94=99=E8=AF=AF=E7=9A=84?= =?UTF-8?q?=E6=AD=A3=E5=88=99=E8=A1=A8=E8=BE=BE=E5=BC=8F=20=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E6=96=87=E4=BB=B6=E5=A4=B9=E4=B8=8A=E4=BC=A0=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=20*=E6=96=87=E4=BB=B6=E5=A4=B9=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E4=BD=BF=E7=94=A8=E4=BA=86webkitdirectory?= =?UTF-8?q?=EF=BC=8C=E6=95=85Firefox=20android=E7=89=88=E4=BB=A5=E5=8F=8AI?= =?UTF-8?q?OS=E8=AE=BE=E5=A4=87=E4=B8=8D=E6=94=AF=E6=8C=81=E8=BF=99?= =?UTF-8?q?=E4=B8=80=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/HomeController.php | 5 +++-- models/UploadForm.php | 7 ++++--- views/home/index.php | 2 +- web/js/home_script.js | 29 ++++++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/controllers/HomeController.php b/controllers/HomeController.php index 42d23a5..9fa035f 100644 --- a/controllers/HomeController.php +++ b/controllers/HomeController.php @@ -15,7 +15,7 @@ use yii\web\UploadedFile; class HomeController extends Controller { - protected string $pattern = '/^[^\p{C}\/:*?"<>|\\\\]+$/u'; + protected string $pattern = '/^[^\p{C}:*?"<>|\\\\]+$/u'; public function behaviors() { @@ -261,6 +261,7 @@ class HomeController extends Controller /** * 文件上传 + * 注意,已存在的同名文件会被覆盖 * https://devs.chenx221.cyou:8081/index.php?r=home%2Fupload * * @return string|Response @@ -275,7 +276,7 @@ class HomeController extends Controller foreach ($uploadedFiles as $uploadedFile) { $model->uploadFile = $uploadedFile; - if (!preg_match($this->pattern, $model->uploadFile->baseName)) { + if (!preg_match($this->pattern, $model->uploadFile->fullPath)) { continue; } if ($model->upload()) { diff --git a/models/UploadForm.php b/models/UploadForm.php index 88e4c19..f78ce21 100644 --- a/models/UploadForm.php +++ b/models/UploadForm.php @@ -35,9 +35,10 @@ class UploadForm extends Model if (!is_dir($absolutePath)) { return false; } - $fileName = $this->uploadFile->baseName; - if ($this->uploadFile->extension !== '') { - $fileName .= '.' . $this->uploadFile->extension; + $fileName = $this->uploadFile->fullPath; + $directory = dirname($absolutePath . '/' . $fileName); + if (!is_dir($directory)) { + mkdir($directory, 0777, true); } $this->uploadFile->saveAs($absolutePath . '/' . $fileName); return true; diff --git a/views/home/index.php b/views/home/index.php index 4710726..c4ea9d7 100644 --- a/views/home/index.php +++ b/views/home/index.php @@ -7,7 +7,6 @@ /* @var $directory string 当前路径 */ use app\models\RenameForm; -use app\models\UploadForm; use yii\bootstrap5\ActiveForm; use yii\bootstrap5\Html; use app\assets\FontAwesomeAsset; @@ -35,6 +34,7 @@ $this->registerCssFile('@web/css/home_style.css');