我试图从控制器类中的$ _FILES数组获取文件名和临时路径, 我使用$ _FILES ['image']来获取控制器类中的文件名,但它打印为空。 但是当我使用var_dump打印整个$ _FILES时,它会打印文件名temp path。
我的控制器代码,
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',
);
}
}
答案 0 :(得分:0)
为什么不在 Yii STYLE
中执行此操作 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'];
答案 1 :(得分:0)
使用 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);
}
了解更多信息; Yii2 UploadedFile