/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new StudentDocument(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['StudentDocument'])) { $model->attributes = $_POST['StudentDocument']; if ($model->save()) { $this->redirect(array('view', 'id' => $model->id)); } } $this->render('create', array('model' => $model)); }
public function actionDocument() { //echo $_POST['StudentDocument']['sid'];exit; $model = new StudentDocument(); $flag = 1; $valid_file_types = array('image/jpeg', 'image/png', 'application/pdf', 'application/msword', 'text/plain'); // Creating the array of valid file types $files_not_saved = ''; // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['StudentDocument'])) { $list = $_POST['StudentDocument']; $no_of_documents = count($list['title']); // Counting the number of files uploaded (No of rows in the form) for ($i = 0; $i < $no_of_documents; $i++) { $model = new StudentDocument(); $model->student_id = $_POST['StudentDocument']['student_id'][$i]; $model->title = $_POST['StudentDocument']['title'][$i]; $extension = end(explode('.', $_FILES['StudentDocument']['name']['file'][$i])); // Get extension of the file $model->file = $this->generateRandomString(rand(6, 10)) . '.' . $extension; // Generate random string as filename $model->file_type = $_FILES['StudentDocument']['type']['file'][$i]; $model->is_approved = 0; $model->uploaded_by = Yii::app()->user->Id; $file_size = $_FILES['StudentDocument']['size']['file'][$i]; if ($model->student_id != '' and $model->title != '' and $model->file != '' and $model->file_type != '') { if (in_array($model->file_type, $valid_file_types)) { if ($file_size <= 5242880) { if (!is_dir('uploadedfiles/')) { mkdir('uploadedfiles/'); } if (!is_dir('uploadedfiles/student_document/')) { mkdir('uploadedfiles/student_document/'); } if (!is_dir('uploadedfiles/student_document/' . $model->student_id)) { mkdir('uploadedfiles/student_document/' . $model->student_id); } $temp_file_loc = $_FILES['StudentDocument']['tmp_name']['file'][$i]; $destination_file = 'uploadedfiles/student_document/' . $model->student_id . '/' . $model->file; if (move_uploaded_file($temp_file_loc, $destination_file)) { if ($model->save()) { $flag = 1; } else { $flag = 0; if (file_exists($destination_file)) { unlink($destination_file); } $files_not_saved = $files_not_saved . ', ' . $model->file; Yii::app()->user->setFlash('errorMessage', UserModule::t("File(s) " . $files_not_saved . " was not saved. Please try again.")); continue; } } else { $flag = 0; $files_not_saved = $files_not_saved . ', ' . $model->file; Yii::app()->user->setFlash('errorMessage', UserModule::t("File(s) " . $files_not_saved . " was not saved. Please try again.")); continue; } } else { $flag = 0; Yii::app()->user->setFlash('errorMessage', UserModule::t("File size must not exceed 5MB!")); } } else { $flag = 0; Yii::app()->user->setFlash('errorMessage', UserModule::t("Only files with these extensions are allowed: jpg, png, pdf, doc, txt.")); } } elseif ($model->title == '' and $model->file_type != '') { $flag = 0; Yii::app()->user->setFlash('errorMessage', UserModule::t("Document Name cannot be empty!")); //$this->redirect(array('create','model'=>$model,'id'=>$_REQUEST['id'])); } elseif ($model->title != '' and $model->file_type == '') { $flag = 0; Yii::app()->user->setFlash('errorMessage', UserModule::t("File is not selected!")); } elseif ($model->student_id == '' and $model->title == '' and $model->file == '' and $model->file_type == '') { $flag = 1; } } if ($flag == 1) { $this->redirect(array('profile')); } else { $this->redirect(array('profile')); } } // END isset /* $this->render('create',array( 'model'=>$model, ));*/ }