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'); ?>