Yii 2对字符串调用成员函数saveAs()

guz6ccqo  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(158)

新的Yii和我得到这个错误的发送电子邮件页.字符串的问题是路径附件和附件名称,我假设,saveAs函数将期待一个字符串.任何想法,我错过了什么?
表单:

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

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

<div class="emails-form">

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

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

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

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

    <?= $form->field($model, 'content')->textarea(['rows' => 6]) ?>

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

    <div class="form-group">
        <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

以及控制器:

public function actionCreate()
    {
        $model = new emails();

        if ($model->load(Yii::$app->request->post())) {

            // upload the attachment
            $model->attachment = UploadedFile::getInstance($model, 'attachment');

            if($model->attachment)
            {
                parent::init();
                $time = time();
                //$model->attachment->saveAs('attachments/'.$time.'.'.$model->attachment->extension);
                //$model->attachment = 'attachments/'.$time.'.'.$model->attachment->extension;
            }
            if($model->attachment)
            {
                $value = Yii::$app->mailer->compose()
                ->setFrom(['my_email@gmail.com' => 'Paul'])
                ->setTo ($model->receiver_email)
                ->setSubject ($model->subject)
                ->setHtmlBody ($model->content);
                foreach ($model->attachment as $file) {
                    //$filename = 'attachments/'.$time.'.'.$model->attachment->extension;
                    $filename = 'attachments/file.jpg';
                    //var_dump($filename);die();
                    $file->saveAs($filename);
                    $value->attach('attachments/file.jpg');
                    //$value->attach('attachments/'.$time.'.'.$model->attachment->extension);
                }
                $value->send();
            }else{
             $value = Yii::$app->mailer->compose()
             ->setFrom(['my_email@gmail.com' => 'Paul'])
             ->setTo($model->receiver_email)
             ->setSubject($model->subject)
             ->setHtmlBody($model->content)
             ->send();
         }

         $model->save();
         return $this->redirect(['view', 'id' => $model->id]);
     }

     return $this->render('create', [
        'model' => $model,
    ]);
 }

我尝试过绝对路径和动态路径,但都有相同的输出,我被卡住了。

3gtaxfhh

3gtaxfhh1#

保存为()函数需要实际路径。因此这是个问题。请确保您的路径正确且可访问。

相关问题