2016-10-25 14:14:22 2557次浏览 2条回答 0 悬赏 30 金钱

我的视图:

/view/ext/_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model app\models\Ext */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="ext-form">

    <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?> 

    <?= $form->field($model, 'image1')->fileInput() ?>

    <?= $form->field($model, 'image2')->fileInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'info')->textInput(['maxlength' => true]) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? '创建' : '更新', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
    <?php ActiveForm::end(); ?>
</div>

这是model
models/ext.php

<?php
namespace app\models;
use Yii;
use yii\web\UploadedFile;
/**
 * This is the model class for table "ext".
 *
 * @property integer $id
 * @property string $image1
 * @property string $image2
 * @property string $info
 */
class Ext extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'ext';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['image1', 'image2'], 'string', 'max' => 60],
            [['image1', 'image2'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
            [['info'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'image1' => 'logo图片',
            'image2' => '微信二维码图片',
            'info' => '站点简介',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function upload()
    {        
        if ($this->validate()) 
        {
            $randName=date("Y").date("m").date("d").date("H").date("i").date("s").rand(100, 999).'-'.md5(microtime());   //生成随机文件名
            $uploadPath= 'uploads/' . $randName . '.' . $this->image1->extension;  //设置保存路径, 为 backend\web\uploads
            $this->image1->saveAs($uploadPath);  //保存文件
            return Url::to('app/web/'.$uploadPath,true); //返回文件的 url 路径,使用 Url 帮助类。
        } 
        else 
        {            
            return false;
        }
    }
}

控制器:
controllers/Extcontroller.php

<?php
namespace app\controllers;
use Yii;
use app\models\Ext;
use app\models\ExtSearch;
use yii\web\Controller;
use yii\web\UploadedFile;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

class ExtController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Ext models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ExtSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Ext model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        ......
    }

    /**

    public function actionCreate()
    {
        $model = new Ext();
        //$model->wb_logo = UploadedFile::getInstance($model, 'image1');
        if ($model->load(Yii::$app->request->post()) ) {
            //var_dump(Yii::$app->request->post());die;
            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    protected function findModel($id)
    {
        if (($model = Ext::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    public function actionUpload()
    {
        $model = new Ext();

        if (Yii::$app->request->isPost) {
            $model->image1 = UploadedFile::getInstance($model, 'image1');
            if ($model->upload()) {
                // 文件上传成功
                return;
            }
        }
        return $this->render('upload', ['model' => $model]);
    }
}

现在就是一直成功不了

最佳答案

您需要登录后才可以回答。登录 | 立即注册
kokocool
总监

kokocool

注册时间:2016-10-31
最后登录:2017-03-20
在线时长:5小时6分
  • 粉丝6
  • 金钱1580
  • 威望50
  • 积分2130

热门问题