Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Yii2 UploadedFile::getInstance()返回null_Yii2_Yii2 Advanced App - Fatal编程技术网

Yii2 UploadedFile::getInstance()返回null

Yii2 UploadedFile::getInstance()返回null,yii2,yii2-advanced-app,Yii2,Yii2 Advanced App,发送我的表单时,UploadedFile::getInstance($model,'images')返回null。我还尝试了上传文件::getInstanceByName('images')。在$\u POST数组中,images键为空,例如'images'=>[''']。该文件存在于$\u FILES数组中 我的代码非常简单。我的看法是: <?php $form = ActiveForm::begin([ 'options' => [ 'class' =&g

发送我的表单时,
UploadedFile::getInstance($model,'images')
返回
null
。我还尝试了
上传文件::getInstanceByName('images')
。在
$\u POST
数组中,
images
键为空,例如
'images'=>[''']
。该文件存在于
$\u FILES
数组中

我的代码非常简单。我的看法是:

<?php $form = ActiveForm::begin([
    'options' => [
        'class' => 'validation-wizard wizard-circle floating-labels',
        'enctype'=>'multipart/form-data'
    ],
]); ?>

<?= $form->field($model, 'images[]')->fileInput([
    'id' => 'image_0',
    'class' => 'dropify',
    'data-default-file' => ''
]) ?>

<?php ActiveForm::end() ?>

如果要访问文件数组,则需要使用而不是
UploadedFile::getInstance()


处理多个文件的好例子可以在第节的指南中找到。

为什么使用
图像[]
作为字段名而不是
图像
?因为我需要多个图像。但也尝试了在单个图像中使用不带括号的whit
images
。结果是一样的。然后你应该使用
getInstances()
而不是
getInstance()
。效果很好。多谢各位。你可以把你的答案贴在下面,让我批准。
public $images;
public function rules()
{
    return [
        ['images', 'each', 'rule' => ['file']],
    ];
}
$files = UploadedFile::getInstances($model, 'images');