上传文件yii2没有扩展

时间:2016-09-02 17:59:02

标签: php file-upload yii2 image-uploading

我正在尝试上传文件而不使用任何扩展名,但结果不成功。例如弹出的成功上传在屏幕上显示为空白,我不知道为什么。

这是我的代码siteController.php

public function actionUpload()
{
    $model = new \app\models\UserPhoto();
    if (\Yii::$app->request->post()) {
        $model->photo = \yii\web\UploadedFile::getInstance($model, 'photo');
        $model->user_id = \Yii::$app->user->id;
        if ($model->validate()) {
            $saveTo = 'uploads/' . $model->photo->baseName . '.' . $model->photo->extension;
            if ($model->photo->saveAs(saveTo)) {
                $model->save(false);
                Yii::$app->session->setFlash('success', 'foto berhasil di upload');
            }
        }
    }
    return $this->render('upload', ['model' => $model]);
}

site \ upload.php的此代码

<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
?>
<h1>Upload Foto</h1>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'] ]) ?>
    <?= $form->field($model, 'photo')->fileInput() ?>
<div class="form-group">
    <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php
ActiveForm::end();
?>

这个代码用于models \ UserPhoto.php

public function rules()
{
    return [
        [['user_id', 'photo'], 'required'],
        [['user_id'], 'integer'],
        [['photo'], 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 1024*1024]
    ];
}

2 个答案:

答案 0 :(得分:1)

您的代码saveTo中有错误,但不会回发给模态。

if ($model->photo->saveAs(saveTo))应替换为if ($model->photo->saveAs($saveTo))

答案 1 :(得分:0)

这是我的控制器:

public function actionUpload_banner() {
    $model = new \app\models\my_model();
    $uploadedFile = UploadedFile::getInstance($model, 'banner_img');
    $model->banner_img = UploadedFile::getInstance($model, 'banner_img');
    if (!empty($model->banner_img)) {
        $myfile = rand(100, 1000) . '_' . $uploadedFile;
        $model->banner_img->saveAs('uploads/' . $myfile);
    } else {
        echo "Please Select File"; // if empty handle situation 
    }
    $model->banner_img = $myfile;
    if (!empty($model->banner_img)) {
        $model->save(FALSE);         // Save data in db

    }
}

这是我的form.php

<div class="modal-body">
            <div class="row">
                <?php $form = ActiveForm::begin(['action' => [Yii::getAlias('@COMPANY_URL') . '/clients/upload_banner?client_id=' . $client_id], 'id' => 'banner_form', 'options' => ['enctype' => 'multipart/form-data']]); ?>
                <section class="col-sm-4">
                    <label><b>Select Banner Image : </b></label>
                </section>
                <section class="col-sm-8">
                    <?= $form->field($clientModel, 'banner_img')->fileInput()->label(false) ?>
                </section>
                <section class="col-sm-6">
                    <?= Html::submitButton($clientModel->isNewRecord ? 'Upload' : 'Update', ['class' => $clientModel->isNewRecord ? 'btn btn-success' : 'btn btn-primary', 'id' => 'banner_save_btn']) ?>
                    <a class="btn btn-danger txt-color-white" role="button" id="banner_cancel" >Cancel </a>
                </section>
                <?php ActiveForm::end(); ?>
            </div><br>
        </div>

这是我的模特:

[['banner_img'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
['banner_img', 'required']