在Yii框架中从$_FILES获取文件名

nszi6y05  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(164)

我试图从控制器类的$_FILES数组中获取文件名和临时路径,我使用$_FILES ['image']获取控制器类中的文件名,但它打印为空。但当我使用var_dump打印整个$_FILES时,它打印文件名和临时路径。
MY控制器代码:

public function actionUpload()
{
        $model=new UploadModel();
        echo var_dump($_FILES);
        echo "image-->".$_FILES['image'];

}

我模型代码,

<?php
class UploadModel extends CFormModel 
{
  public $image;
    // ... other attributes

    public function rules()
    {
              return array(
                'image','file',
                        'safe'=>true,
                        'allowEmpty'=>TRUE,
                        'maxSize'=>512000,
                        'types'=>'csv',
                        'tooLarge'=>'The size of the file is more than 100Kb',
                        'wrongType'=>'the file must be in the jpeg,png format',
            );
    }
    public function attributeLabels()
    {
        return array(
            'image'=>'image',
        );
    }
}
fivyi3re

fivyi3re1#

为什么不这样做在Yii风格

public function actionUpload()
    {
            $model=new UploadModel();
    //rest of your code

            $model->image=  CUploadedFile::getInstance($model, 'image');
            if(!empty($model->image))
            {
                echo $model->image->name . $model->image->size;
            }

//render view here
    }

规则

'tooLarge'=>'The size of the file is more than 250Kb',
'wrongType'=>'the file must be in the csv format',

如果情况是这样的,你想做的Yii风格然后

用这个

echo "image-->".$_FILES['image']['name'];
bsxbgnwa

bsxbgnwa2#

使用UploadedFile模块,它有几个属性

if ($model->load(Yii::$app->request->post())) {
            // get instances of uploaded files
            $model->english_file = UploadedFile::getInstance($model, 'english_file');
            $model->english_file->saveAs('uploads/' . $model->english_file->name);
}

获取更多信息; Yii 2上传文件

相关问题