0

when i upload multiple files in Yii2, i am getting following error and cannot insert data into database:

finfo_file(C:\xampp\tmp\phpCACD.tmp): failed to open stream: No such file or directory

controller file:

public function actionGallery($id)
    {
        $model = new Gallery();
        if (Yii::$app->request->post()) {
            $model->imageFiles = UploadedFile::getInstances($model,'imageFiles');    
            foreach ($model->imageFiles as $file) {
                $imageName = Yii::$app->security->generateRandomString();
                $model->added_date = date('Y-m-d');
                $model->emp_id = $id;
                $file->saveAs('uploads/emp/' . $imageName . '.' . $file->extension);
                $originalFile = EMP_PROFILE_ORIGINAL.$imageName.'.'.$file->extension;
                $thumbFile = EMP_PROFILE_THUMB.$imageName.'.'.$file->extension;
                Image::thumbnail($originalFile, 200, 200)->save($thumbFile, ['quality' => 80]);
                $model->save();
            }
        }
        return $this->render('gallery', [
            'gal' => $model
            ]);
    }

view file:

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\grid\GridView;
?>

<div class="posts-form">
    <div class="wrapper-md">
        <div class="row">
            <div class="col-sm-8">
                <div class="panel panel-default">
                    <div class="panel-body">

                        <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
                        <?= $form->field($gal, 'imageFiles[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>
                        <div class="form-group">
                            <button class="btn btn-success">Submit</button>
                            <a class="btn btn-default" href="<?=\Yii::$app->urlManager->createUrl('post')?>">Cancel</a>
                        </div>
                        <?php ActiveForm::end(); ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

but i am getting above error. I cannot find the solution for this.

1
  • I think $model = new Gallery(); should be inside foreach() loop Commented Jul 25, 2016 at 6:16

4 Answers 4

2

first, in model you must have 2 variable that will save the images and the name of it.

`
* @property string $image_name
*/
class Gallery extends \yii\db\ActiveRecord
{
    **public $fileImage=[];**
    public static function tableName(){
`

in this case I use $image_name that is one of my model column, and $fileImage, $fileImage is an array that will be used to upload the image,

:)

then in controller

$model = new Gallery(); // this variable is only used to check if everything is valid
if ($model->load(Yii::$app->request->post())) {
    $model->fileImage = UploadedFile::getInstances($model,'fileImage');
    $a=0;
    foreach ($model->fileImage as $file) {
        $a++;
        $model1 = new Gallery();
        $file->saveAs("images/test".$a.".".$file->extension);
        $model1->image_url="images/test.".$a.".".$file->extension;
        $model1->image_name = "".$a;

        $model1->save();
    }

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

I think thats all. . .

Sign up to request clarification or add additional context in comments.

4 Comments

i will try and back to you
images are going to the folder but not in db
the image name save in $model1->image_url,,,, image_url is a field in my database that used to save the name of that files
sorry, I have no internet access yesterday :)
1

You can use

$sql = 'INSERT INTO `table`(`id`, `col1`, `col2`, `path`) VALUES (Null,"'.($model2->col1).'","'.($model2->col2).'","'.($model2->path).'")';
                $command = \Yii::$app->db->createCommand($sql);
                $command->execute();

instead of $model->save();

I made an example, so the whole code in controller would be:

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

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

             $model->file = UploadedFile::getInstances($model, 'file');
            foreach ($model->file as $file) {

            $model2 = new Planet();

            $model2->load(Yii::$app->request->post());
             $model2->path='uploads/' . $file;

            $sql = 'INSERT INTO `planet`(`id`, `name`, `s_id`, `path`) VALUES (Null,"'.($model2->name).'","'.($model2->s_id).'","'.($model2->path).'")';
            $command = \Yii::$app->db->createCommand($sql);
            $command->execute();
                $file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);

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

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

It works for me.

edit: also you can use $model2->save(false); It worked in my case.

1 Comment

this is the temporary solution
0

Call $model->save()

before

$model->file->saveAs();

I was having the exact same problem

1 Comment

already tried it but not working.. it is in foreach loop.. i want to save name of image into database also
-1

*I think you forget to create uploads and emp folder or maybe you use wrong folder name, please check your folder name...

*If you want to save the name of that file, you should use different variable (two variable) that will be used to save the images and the name of that images,

3 Comments

folder name is right. problem is that first image is uploaded into the folder but not going to save the imagename in database. and it raise the error when trying to upload second image.
may I see your code in the form? in $model->imageFiles = UploadedFile::getInstances($model,'imageFiles'); What is the ImageFiles data-types??, is $file->saveAs('uploads/emp/' . $imageName . '.' . $file->extension); work? because you use $file that never declare before, you said that you want to save multiple image name in your model, is the name will be saved in different row? if yes, you should declare $model = new Gallery();
is the name of the file will save in different row? use this inside your foreach if yes $model = new Gallery(); are you have another variable in model that will used to save the name of the image? because I think you need that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.