Yii Framework Forum: Can't Upload Image By Using Kartik's Fileinput In Yii2.0 - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Can't Upload Image By Using Kartik's Fileinput In Yii2.0 Rate Topic: ***** 1 Votes

#1 User is offline   fhqrnr 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 24-May 14

Posted 03 June 2014 - 09:25 AM

The view:

<?php

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


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

<div class="row">
    <div class="col-md-9">  
        <?php
        $form = ActiveForm::begin([
                    'id' => 'login-form-horizontal',
                    'type' => ActiveForm::TYPE_HORIZONTAL,
                    'formConfig' => ['labelSpan' => 3, 'deviceSize' => ActiveForm::SIZE_SMALL,
                    'options' => ['enctype' => 'multipart/form-data']]
        ]);
        ?>
        <div class="panel panel-default">
            <div class="panel-body">

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

                <?= $form->field($model, 'category_id')->textInput() ?>

                <?php
                echo $form->field($model, 'gallery')->widget(FileInput::classname(), [
                    'options' => ['multiple' => true, 'accept' => 'image/*'],
                    'pluginOptions' => [
                        'previewFileType' => 'image',
                        'showUpload' => false
                    ],
                ]);
                ?>
                
                <?= $form->field($model, 'description')->textInput(['maxlength' => 2000]) ?>

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

                <?= $form->field($model, 'duration')->textInput() ?>

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


            </div>
        </div>
        <div class="form-group" style="text-align: center">
            <?= Html::submitButton($model->isNewRecord ? 'save&next' : 'update&next', ['class' => $model->isNewRecord ? 'btn btn-lg btn-success' : 'btn btn-lg btn-primary']) ?>
        </div>
        <?php ActiveForm::end(); ?>
    </div>

</div>




The controller:

    public function actionCreate() {
        if (Yii::$app->user->isGuest) {
            return Yii::$app->user->loginRequired();
        } else {
            $model = new Service;
            $model->user_id = Yii::$app->user->getId();
            $model->created_at = date_timestamp_get(date_create());
            $model->updated_at = date_timestamp_get(date_create());
            
            $file = UploadedFile::getInstance($model, 'gallery');
                
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                $file->saveAs('/real/path/to/postimages/');
                return $this->redirect(['view', 'id' => $model->id]);
            } else {
                return $this->render('create', [
                            'model' => $model,
                ]);
            }
        }
    }



The error screenshot:

Attached File  6d535f7419898bbb9d1083be50a32569.png (186.93K)
Number of downloads: 39



i don't know where was wrong, please help
0

#2 User is offline   fhqrnr 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 24-May 14

Posted 03 June 2014 - 08:26 PM

anyboay can help?
0

#3 User is offline   NoBiTa99 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 22-August 13

Posted 08 June 2014 - 12:59 PM

My problem is the same and i also can't fix it :(
0

#4 User is offline   Kartik V 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 629
  • Joined: 29-August 12

Posted 05 July 2014 - 04:22 AM

You can follow the steps in this article for uploading a file in Yii2 using the kartik\widgets\FileInput widget.

Secondly, I see that you are using multiple file uploads (multiple property is set to true in the widget). You must use an array naming format for multiple file uploads as mentioned in this article.
0

#5 User is offline   cms 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 12-September 14

Posted 23 September 2014 - 04:47 AM

added this below code

view page

<?= $form->field($model, 'photo')->widget(FileInput::classname(), [
'options' => ['accept' => 'photo/*'],
]);
?>


controller page

Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/uploads/';
if ($model->load(Yii::$app->request->post())) {
$image = UploadedFile::getInstance($model, 'photo');
$ext = end((explode(".", $image->name)));
$model->photo = time().$model->id.".{$ext}";
$path = Yii::$app->params['uploadPath'] . $model->photo;

if($model->save()){
$image->saveAs($path);

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

return $this->render('create', [
'model' => $model,

]);
}


model page

[['photo'], 'safe'],
[['photo'], 'file', 'extensions'=>'jpg, gif, png'],
0

#6 User is offline   Kartik V 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 629
  • Joined: 29-August 12

Posted 11 October 2014 - 03:53 AM

Refer this follow-up wiki for details on how you can use the FileInput for preview, update, and delete.
0

#7 User is offline   LeVanLau 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 84
  • Joined: 24-April 14

Posted 02 November 2014 - 05:32 AM

Hi,
I face similar issue too.
UploadFile::getInstance($model,'image') is always return NULL.
In model have : public $image, [['image'], 'safe'],[['image'], 'file', 'extensions'=>'jpg, png'],
In ActiveForm have : 'options' => ['enctype' => 'multipart/form-data']].
In image ActiveField have : 'options' => ['accept' => 'image/*'].
I can not know where I am wrong while I did all tutorials from :
- http://webtips.kraje...leinput-widget/
- http://webtips.kraje...file-input-php/
- http://webtips.kraje...leinput-widget/
Anybody have any other ideas about this? Thanks.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users