谁一开始就是大神?

yii2.0basic图片上传不了问题[2.0]

我的视图:

/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]);
    }
}

现在就是一直成功不了

[[‘image1’, ‘image2’], ‘string’, ‘max’ => 60],
[[‘image1’, ‘image2’], ‘string’, ‘max’ => 60],

赞(0) 打赏
未经允许不得转载:菜鸟之家 » yii2.0basic图片上传不了问题[2.0]

评论 抢沙发

登录

找回密码

注册