balx 2016-11-22 02:09:06 2824次浏览 0条评论 2 0 0
class UploadForm extends Model
{
    /**
     * @var UploadedFile
     */
    public $img_alipay;
    public $img_wxpay;

    public function rules()
    {
        return [
            [['img_alipay'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, gif, jpg'],
            [['img_wxpay'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, gif, jpg'],
        ];
    }

    public function upload()
    {
        if ($this->validate()) {
            if($this->img_alipay){
                $this->img_alipay->saveAs(Yii::getAlias('@frontend').'/web/uploads/' . $this->img_alipay->baseName . '.' . $this->img_alipay->extension);
            }
            if($this->img_wxpay){
                $this->img_wxpay->saveAs(Yii::getAlias('@frontend').'/web/uploads/' . $this->img_wxpay->baseName . '.' . $this->img_wxpay->extension);
            }
            return true;
        } else {
            return false;
        }
    }
}
public function actionCreate()
{
    $model = new UserLevel();
    if ($model->load(Yii::$app->request->post())) {
        $modelUpload = new UploadForm();
        $modelUpload->img_alipay = UploadedFile::getInstance($model, 'img_alipay');
        $modelUpload->img_wxpay = UploadedFile::getInstance($model, 'img_wxpay');
        if ($modelUpload->upload()) {
            if($modelUpload->img_alipay){
                $model->img_alipay = '/uploads/' . $modelUpload->img_alipay;
            }
            if($modelUpload->img_wxpay){
                $model->img_wxpay = '/uploads/' . $modelUpload->img_wxpay;
            }
        }
        if (!$model->save()) {
            return $this->redirect('error');
        }else{
            return $this->redirect(['view', 'id' => $model->id]);
        }
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册