SHARE
TWEET

UserController

a guest May 22nd, 2017 113 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public function actionUpload()
  2.     {
  3.         $model = new User;
  4.  
  5.         if ($model->load(Yii::$app->request->post())) {
  6.             $model->file = UploadedFile::getInstance($model, 'file');
  7.             if($model->file) {
  8.                 $handle = fopen($model->file->tempName, 'r');
  9.                
  10.                 while( ($line = fgetcsv($handle, 1000, ";")) != FALSE) {
  11.                     $password = $model->setPassword($line[5]);
  12.                     $email = $line[4];
  13.                     $username = $line[4];
  14.                    
  15.                     $datas[] = array(
  16.                         'username' => $username,
  17.                         'email' => $email,
  18.                         'password_hash' => $password,
  19.                     );
  20.                 }
  21.                 fclose($handle);
  22.  
  23.                 $tableName = 'user';
  24.                 $columnNameArray = ['username','email','password_hash'];
  25.  
  26.                 Yii::$app->db->createCommand()->batchInsert($tableName, $columnNameArray, $datas)->execute();
  27.  
  28.                 return $this->redirect(['index']);
  29.             }
  30.         } else {
  31.             return $this->render('upload', [
  32.                 'model' => $model,
  33.             ]);
  34.         }
  35.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top