donglisi8644 2017-04-19 12:39
浏览 186
已采纳

我使用yii2高级,文件上传不起作用

here in my controller

<?php
namespace backend\controllers;
use Yii;
use backend\models\Vendors;
use backend\models\User;
use app\models\VendorsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use frontend\models\SignupForm;
use backend\models\UserProfiles;
use backend\models\Testuploadmodel;
use yii\web\UploadedFile;
class VendorsController extends Controller
{        
public function actionUpdate($id, $user_id)
{
    $Testuploadmodel = new Testuploadmodel();
    if ($Testuploadmodel->load(Yii::$app->request->post())) {
        $path = Yii::getAlias('@frontend') .'/web/uploads/';
        $Testuploadmodel->covers = 
        UploadedFile::getInstance($Testuploadmodel, 'covers');
        if ($Testuploadmodel->covers && $Testuploadmodel->validate()) {
            $Testuploadmodel->covers->saveAs($path . $Testuploadmodel->covers->baseName . '.' . $Testuploadmodel->covers->extension);
        }
        return $this->redirect(['view', 'id' => $id, 'user_id' => $user_id]);
    }
    else {
        return $this->render('update', [
            'Testuploadmodel' => $Testuploadmodel,
        ]);
    }
}

Here is my Model

<?php
namespace backend\models;
use yii\base\Model;
use yii\web\UploadedFile;
class Testuploadmodel extends Model
{
    /**
     * @var UploadedFile
     */
    //public $imageFile;
     public $covers;
    public function rules()
    {
        return [
            [['covers'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 4],
        ];
    }
}
?>

And here is my view file. in view file i have changed different option. i have implemented same functionality in other place which is working, but here it is not working. there, print_r($_POST) show empty array with File field name, but here the File field name array shows images name. can't find a way to resolve.

<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
$this->params['breadcrumbs'][] = 'Update';
?>
<h1><?= Html::encode($this->title) ?></h1>
<?php $form = ActiveForm::begin(['id' => 'registration-form', 'options' => 
['enctype' => 'multipart/formdata']]); ?>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#home">Update Vendor</a></li>
<li><a data-toggle="tab" href="#menu1">Update Profile</a></li>
</ul>
<div class="tab-content">
<div id="home" class="tab-pane fade in active">
    <h3>Update Vendor</h3>
</div>
<div id="menu1" class="tab-pane fade">
    <h3>Update Profile</h3>
    <?php echo  $form->field($Testuploadmodel, 'covers')->fileInput() ?>
</div>
</div>
<div class = "form-group">
<?php echo  Html::submitButton('Submit', ['class' => 'btn btn-primary',
    'name' => 'profile-button']) ?>
</div>
<?php ActiveForm::end(); ?>
  • 写回答

2条回答 默认 最新

  • dongqigu0429 2017-04-20 06:51
    关注

    Problem was spelling mistake in "multipart/form-data". I have written "multipart/formdata" which is incorrect. there should be a "-" dash between form and data. That's why Files were not uploading.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”