43 thoughts on “Yii2 Lesson – 17 Uploading Files to the server

  1. i want to upload image to database but the image just stored in folder and not saved in database. i dont understand what is the problem, please help me,

  2. Unknown Property – yiibaseUnknownPropertyException
    Getting unknown property: appmodelsCalonPegawaiM::calonpegawaim

  3. Great Tutorials,but only One suggestion that Please share the code of every tutorial somewhere on your blog/website or anywhere from where we can copy and practice the code.

  4. Hello Sir ,
    as you are coding same i am doing everything is correct there is no any error image is saved in folder but as i refresh my database i can't see the image name as you mentioned in the line of code
    as: $imagename=$model->F_Name;
    $model->file=UploadedFile::getInstance($model,'Image');
    $model->file->saveAs('Images/'.$imagename.'.'.$model->file->extension);
    $model->file='Images/'.$imagename.'.'.$model->file->extension;
    please help me out in this case..

  5. Hi, I'm following the tuturials, this it works fine. Can u add how display and resize the image file in the view ? Thanks

  6. I can add an image by updating a record (using Larry Pham's code) but not when creating a new record. It uploads the file, but the logo field is just null. No errors, it just won't update the record.

  7. Hello i got this error 🙁
    PHP Warning – yiibaseErrorException

    move_uploaded_file(/doc_123123.docx): failed to open stream: Permission denied

  8. helloi igat this error
    PHP Fatal Error – yiibaseErrorException
    Call to a member function formName() on string
    public static function getInputName($model, $attribute)

    $formName = $model->formName();
    if (!preg_match('/(^|.*])([w.]+)([.*|$)/', $attribute, $matches)) {
    throw new InvalidParamException('Attribute name must contain word characters only.');

  9. Thx for tutorial.
    I use this tutorial for profile page (amnah/yii2-user) and get "Call to a member function saveAs() on null", after put saveAs inside "if" error gone, but every profile update (nickname change for example) make new link inside db – without "/images" and extension. After add "save the path in the DB column" string inside if – all become works fine: if($model->file !== null)
    {
    $model->file->saveAs('images/'.$imageName.'.'.$model->file->extension);
    // save the path in the DB column
    $model->logo = 'images/'.$imageName.'.'.$model->file->extension;
    }

  10. hello. can u show me how can i make a uploaded file visible on the the page???
    for ex: viewing copany logo on companies page……..

  11. hi tutorial works smoothly. but when i added something in my activeform::begin. something like this:

    $form = ActiveForm::begin(['options' => [
    'enctype' => 'multipart/form-data'],
    'enableClientValidation' => true,
    'id' => 'someform']);

    it didn't upload the file, i'd view the array and it shows the image file but no size indicated. and when i remove clientvalidation and id. it works fine.

    please help me regarding with this. Thanks.

  12. Hello excellent tutorial, in the action create me works without problems but could they help me to make me work in the update action?

  13. public function actionCreate()
    {
    $model = new Student();

    $imageNpm = $model->student_npm;
    $imageName = $model->student_name;

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

    $model->file = UploadedFile::getInstance($model,'file');
    $model->file->saveAs( 'uploads/'.$imageNpm.'.'.$model->file->extension);

    $model->student_picture = 'uploads/'.$imageNpm.'.'.$model->file->extension;

    $model->student_created_date = date('Y-m-d h:i:s');
    $model->save();
    return $this->redirect(['view', 'id' => $model->student_id]);
    } else {
    return $this->render('create', [
    'model' => $model,
    ]);
    }
    }

    this is my full code but in the db the student_picture only have "uploads/.jpg" same in the folder of uploads the picture is save as "uploads/.jpg" but in the db the student_npm has value of 2015730010

  14. Hello , after i press the CREATE button nothing happens , no error , just nothing….maybe someone can help , I've verified everything and i see no error in the xyntax or anything else

  15. Plz help me I am getting this error. finfo_file(/tmp/php4QQ5g3): failed to open stream: No such file or directory

  16. Hello the error is for ? finfo_file(C:xampptmpphpA0DA.tmp): failed to open stream: No such file or directory tks

  17. Hello. Can You show how upload multiple files(images) by using only php and yii2, without widgets ? Like in this video, but more than one file.

  18. I think you should put your $model->file->saveAs('uploads/' . $imageName '.' . $model->file->extension); and other stuff on model because you are overloading the controller which is not advisable in terms optimization.

Leave a Reply

Your email address will not be published. Required fields are marked *