yii\web\UploadedFile::getInstancesByName PHP Method

getInstancesByName() public static method

This is mainly used when multiple files were uploaded and saved as 'files[0]', 'files[1]', 'files[n]'..., and you can retrieve them all by passing 'files' as the name.
public static getInstancesByName ( string $name ) : UploadedFile[]
$name string the name of the array of files
return UploadedFile[] the array of UploadedFile objects. Empty array is returned if no adequate upload was found. Please note that this array will contain all files from all sub-arrays regardless how deeply nested they are.
    public static function getInstancesByName($name)
    {
        $files = self::loadFiles();
        if (isset($files[$name])) {
            return [new static($files[$name])];
        }
        $results = [];
        foreach ($files as $key => $file) {
            if (strpos($key, "{$name}[") === 0) {
                $results[] = new static($file);
            }
        }
        return $results;
    }

Usage Example

 /**
  * Action to generate the according folder and file structure from an uploaded zip file.
  *
  * @return multitype:multitype:
  */
 public function actionUploadArchive()
 {
     if (Setting::Get('disableZipSupport', 'cfiles')) {
         throw new HttpException(404, Yii::t('CfilesModule.base', 'Archive (zip) support is not enabled.'));
     }
     // cleanup all old files
     $this->cleanup();
     Yii::$app->response->format = 'json';
     $response = [];
     foreach (UploadedFile::getInstancesByName('files') as $cFile) {
         if (strtolower($cFile->extension) === 'zip') {
             $sourcePath = $this->getZipOutputPath() . DIRECTORY_SEPARATOR . 'zipped.zip';
             $extractionPath = $this->getZipOutputPath() . DIRECTORY_SEPARATOR . 'extracted';
             if ($cFile->saveAs($sourcePath, false)) {
                 $this->unpackArchive($response, $sourcePath, $extractionPath);
                 $this->generateModelsFromFilesystem($response, $this->getCurrentFolder()->id, $extractionPath);
             } else {
                 $response['errormessages'][] = Yii::t('CfilesModule.base', 'Archive %filename% could not be extracted.', ['%filename%' => $cFile->name]);
             }
         } else {
             $response['errormessages'][] = Yii::t('CfilesModule.base', '%filename% has invalid extension and was skipped.', ['%filename%' => $cFile->name]);
         }
     }
     $response['files'] = $this->files;
     return $response;
 }
All Usage Examples Of yii\web\UploadedFile::getInstancesByName