File upload yii2文件上载不工作

File upload yii2文件上载不工作,file-upload,yii2,File Upload,Yii2,我遵循了以下指南,但没有上传任何文件。下面是我的配置 \u表单 这是一个for循环,所以我可以得到一个不同记录的数组 <?= $form->field(new UploadForm , "[$count]file")->fileInput()->label(false) ?> 我现在已经注意到了日志中的以下内容。我如何构造它 $_FILES = [ 'UploadForm' => [ 'name' => [

我遵循了以下指南,但没有上传任何文件。下面是我的配置

\u表单

这是一个for循环,所以我可以得到一个不同记录的数组

<?= $form->field(new UploadForm , "[$count]file")->fileInput()->label(false) ?>
我现在已经注意到了日志中的以下内容。我如何构造它

$_FILES = [
    'UploadForm' => [
        'name' => [
            0 => [
                'file' => 'Destin_Dome.jpg'
            ]
            1 => [
                'file' => ''
            ]
            2 => [
                'file' => 'Pelotas_Reprocessing.pdf'
            ]
        ]
        'type' => [
            0 => [
                'file' => 'image/jpeg'
            ]
            1 => [
                'file' => ''
            ]
            2 => [
                'file' => ''
            ]
        ]
        'tmp_name' => [
            0 => [
                'file' => '/tmp/phpoPgbJ9'
            ]
            1 => [
                'file' => ''
            ]
            2 => [
                'file' => ''
            ]
        ]
        'error' => [
            0 => [
                'file' => 0
            ]
            1 => [
                'file' => 4
            ]
            2 => [
                'file' => 1
            ]
        ]
        'size' => [
            0 => [
                'file' => 1129373
            ]
            1 => [
                'file' => 0
            ]
            2 => [
                'file' => 0
            ]
        ]
    ]
]
它似乎无法通过验证,但我能够复制如下:

if(!empty($_FILES['UploadForm']['tmp_name']['file'])){
                        copy($_FILES['UploadForm']['tmp_name']['file'],"/tmp/".$_FILES['UploadForm']['name']['file']);
}
文件上传模型

public $file;
    public $image;
    public $factsheet;


    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [['file'], 'file','maxFiles' => 10],
            [['image'], 'file','extensions' => 'gif, jpg'],
            [['factsheet'], 'file','checkExtensionByMimeType' => false,'extensions' => 'pdf'],
        ];
    }

我可能错了,但我认为问题出在您的控制器中:您的模型规则也适用于多文件上载和表单字段,但在控制器中,您将
$model->file
设置为单个
上载文件
实例。这就是您的验证不起作用的原因。我不能给出你的建议(改变控制器或模型规则),因为我不清楚你在这里做什么。看起来您正在从单个文件上载字段创建多个模型

我正在尝试上载多个文件
$_FILES = [
    'UploadForm' => [
        'name' => [
            0 => [
                'file' => 'Destin_Dome.jpg'
            ]
            1 => [
                'file' => ''
            ]
            2 => [
                'file' => 'Pelotas_Reprocessing.pdf'
            ]
        ]
        'type' => [
            0 => [
                'file' => 'image/jpeg'
            ]
            1 => [
                'file' => ''
            ]
            2 => [
                'file' => ''
            ]
        ]
        'tmp_name' => [
            0 => [
                'file' => '/tmp/phpoPgbJ9'
            ]
            1 => [
                'file' => ''
            ]
            2 => [
                'file' => ''
            ]
        ]
        'error' => [
            0 => [
                'file' => 0
            ]
            1 => [
                'file' => 4
            ]
            2 => [
                'file' => 1
            ]
        ]
        'size' => [
            0 => [
                'file' => 1129373
            ]
            1 => [
                'file' => 0
            ]
            2 => [
                'file' => 0
            ]
        ]
    ]
]
if(!empty($_FILES['UploadForm']['tmp_name']['file'])){
                        copy($_FILES['UploadForm']['tmp_name']['file'],"/tmp/".$_FILES['UploadForm']['name']['file']);
}
public $file;
    public $image;
    public $factsheet;


    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [['file'], 'file','maxFiles' => 10],
            [['image'], 'file','extensions' => 'gif, jpg'],
            [['factsheet'], 'file','checkExtensionByMimeType' => false,'extensions' => 'pdf'],
        ];
    }