public function actionCreatenew() { $model = new ProductsBrands(); $model->scenario = 'productsbrandsupload'; if ($model->load(Yii::$app->request->post())) { $model->FileUpload = UploadedFile::getInstance($model, 'FileUpload'); $k = 0; if ($model->FileUpload) { $time = time(); $model->FileUpload->saveAs(realpath(Yii::$app->basePath) . '/csv/' . $time . '.' . $model->FileUpload->extension); $model->FileUpload = realpath(Yii::$app->basePath) . '/csv/' . $time . '.' . $model->FileUpload->extension; $handle = fopen($model->FileUpload, "r"); $i = 0; $m = 0; while (($fileop = fgetcsv($handle, 1000, ",")) !== false) { if ($i >= 1) { /* products external images */ $model->externalFiles = file_get_contents($fileop[2]); $find = ".'\\'."; $ent = str_replace("/\\/", "/", $fileop[2]); $a = "\\"; $a = preg_replace('/\\\\/', '/', $fileop[2]); $getstrpos = strripos($a, "/"); $imageName = substr($fileop[2], $getstrpos + 1); $time2 = time(); $img = realpath(Yii::$app->basePath) . '/web/uploads/products/' . $time2 . $imageName; $fileimageUrl = 'uploads/products/' . $time2 . $imageName; file_put_contents($img, file_get_contents($fileop[2])); /* $model->externalFiles = UploadedFile::getInstance(file_get_contents($fileop[5])); $time2 = time(); $model->externalFiles->saveAs(realpath(Yii::$app->basePath).'/csv/' .$time2. '.' . $model->FileUpload->extension); $model->externalFiles = realpath(Yii::$app->basePath).'/csv/' .$time2. '.' . $model->FileUpload->extension; */ date_default_timezone_set('Asia/Calcutta'); $model->createdDate = date("Y-m-d H:i:s"); $model->updatedDate = date("Y-m-d H:i:s"); try { $productId = Products::getProductId($fileop[0]); $brandId = Brandsmaster::getBrandId($fileop[1]); $userInfo = Yii::$app->db->createCommand()->insert('products_brands', ['productId' => $productId['id'], 'brandId' => $brandId['brandId'], 'productImage' => $fileimageUrl, 'status' => 'Active', 'createdBy' => Yii::$app->user->identity->id, 'updatedBy' => Yii::$app->user->identity->id, 'createdDate' => $model->createdDate, 'updatedDate' => $model->updatedDate, 'Ipaddress' => $_SERVER['REMOTE_ADDR'] ?: ($_SERVER['HTTP_X_FORWARDED_FOR'] ?: $_SERVER['HTTP_CLIENT_IP'])])->execute(); if ($userInfo) { $m++; } } catch (\Exception $e) { $duplicaterrors = $e->errorInfo[0]; if ($duplicaterrors == 23000) { $k++; } } } $i++; } if ($m > 0) { Yii::$app->getSession()->setFlash('success', 'Your are successfully Uploaded ' . $m . ' Products'); } if ($k > 0) { Yii::$app->getSession()->setFlash('error', 'Your Trying to add ' . $k . ' duplicate records'); } return $this->redirect(['createnew']); } } else { return $this->render('createfile', ['model' => $model]); } }