Api of Multiple File Uploading in Yii2

You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.

« previous (#1)next (#3) »

After getting lot's of error and don't know how to perform multiple images api in yii2 finally I get it today

This is my question I asked on forum and it works for me https://forum.yiiframework.com/t/multiple-file-uploading-api-in-yii2/130519

Implement this code in model for Multiple File Uploading `php public function rules()

    return [
        [['post_id', 'media'], 'required'],
        [['post_id'], 'integer'],
        [['media'], 'file', 'maxFiles' => 10],//here is my file field
        [['created_at'], 'string', 'max' => 25],
        [['post_id'], 'exist', 'skipOnError' => true, 'targetClass' => Post::className(), 'targetAttribute' => ['post_id' => 'id']],
you can add extension or any skiponempty method also

And this is my controller action where I prformed multiple file uploading
public function actionMultiple(){
        $model = new Media;
        $model->post_id = '2';
        if (Yii::$app->request->ispost) {
            $model->media = UploadedFile::getInstances($model, 'media');
            if ($model->media) {
                foreach ($model->media as $value) {
                    $model = new Media;
                    $model->post_id = '2';
                    $BasePath = Yii::$app->basePath.'/../images/post_images';
                    $filename = time().'-'.$value->baseName.'.'.$value->extension;
                    $model->media = $filename;
                    if ($model->save()) {
                return array('status' => true, 'message' => 'Image Saved'); 
        return array('status' => true, 'data' => $model);

If any query or question I will repond