gargron/fileupload插件地址:https://packagist.org/package...
一:gargron/fileupload插件装置
composer require gargron/fileupload
二:应用gargron/fileupload插件实现上传
//设置上传文件格式$magicFile = Yii::getAlias(FileHelper::$mimeMagicFile);$mimeTypes = require($magicFile);$allowExt = ['png', 'jpg'];foreach ($allowExt as $ext) { if (isset($mimeTypes[$ext])) { $allowMimes[] = $mimeTypes[$ext]; }}//验证(最大文件大小2MB,只有两个容许的mime类型)$validator = new \FileUpload\Validator\Simple('2M', $allowMimes);$savePath = 'upload/';//判断是否存在上传目录,不存在创立目录if (!is_dir($savePath)) { FileHelper::createDirectory($savePath);}// 上传文件$pathresolver = new \FileUpload\PathResolver\Simple($savePath);// The machine's filesystem$filesystem = new \FileUpload\FileSystem\Simple();// files为上传控件的name名$fileupload = new \FileUpload\FileUpload($_FILES['files'], $_SERVER);// Adding it all together. Note that you can use multiple validators or none at all$fileupload->setPathResolver($pathresolver);$fileupload->setFileSystem($filesystem);$fileupload->addValidator($validator);// Doing the deedlist($files, $headers) = $fileupload->processAll();foreach($files as $file){ //Remeber to check if the upload was completed if ($file->completed) { echo $file->getRealPath(); // Call any method on an SplFileInfo instance var_dump($file->isFile()); }}
依据如上代码能够实现文件上传