简体   繁体   中英

I am using yii2 advanced, and File uploading is not workings

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(); ?>

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.

"multipart/formdata" is often the basic reason behind file uploading error. Whenever you come across this problem in any platform, first confirm "multipart/formdata".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM