Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. FileController
  2. ++++++++++++++++++++++++++++++++++++++++++++
  3. public function actionCreate()
  4. {
  5. $model=new File;
  6. if(isset($_POST['File']))
  7. {
  8. $model->attributes=$_POST['File'];
  9.  
  10. $model->tip=CUploadedFile::getInstance($model,'tip');
  11.  
  12.  
  13. if($model->validate())
  14. {
  15. $model->save();
  16. $model->tip->saveAs(Yii::app()->basePath . '/../files/' . $model->tip);
  17. }
  18.  
  19.  
  20. $this->redirect(array('view','id'=>$model->id));
  21. }
  22. $this->render('create',array(
  23. 'model'=>$model,
  24. ));
  25. }
  26. Model/File.php
  27. +++++++++++++++++++++++++++++++++++++++++++++++
  28. public function rules()
  29. {
  30. // NOTE: you should only define rules for those attributes that
  31. // will receive user inputs.
  32. return array(
  33. array('nazva, tip', 'required'),
  34. array('nazva', 'length', 'max'=>30),
  35. array('tip', 'length', 'max'=>10),
  36. // The following rule is used by search().
  37. // Please remove those attributes that should not be searched.
  38. array('id, nazva, tip', 'safe', 'on'=>'search'),
  39. array('tip', 'file', 'types'=>'doc, docx, rar, zip, pdf, exe', 'allowEmpty' => true )
  40. );
  41. }
  42. views/file/_form.php
  43. +++++++++++++++++++++++++++++++++++++++++++
  44. <div class="form">
  45.  
  46. <?php $form=$this->beginWidget('CActiveForm', array(
  47. 'id'=>'file-form',
  48. 'enableAjaxValidation'=>false,
  49. 'htmlOptions'=>array('enctype'=>'multipart/form-data')
  50. )); ?>
  51.  
  52. <p class="note">Fields with <span class="required">*</span> are required.</p>
  53.  
  54. <?php echo $form->errorSummary($model); ?>
  55.  
  56. <div class="row">
  57. <?php echo $form->labelEx($model,'nazva'); ?>
  58. <?php echo $form->textField($model,'nazva',array('size'=>30,'maxlength'=>30)); ?>
  59. <?php echo $form->error($model,'nazva'); ?>
  60. </div>
  61.  
  62. <div class="row">
  63. <?php echo $form->labelEx($model,'tip'); ?>
  64. <?php echo CHtml::activeFileField($model, 'tip'); ?>
  65. <?php echo $form->error($model,'tip'); ?>
  66. </div>
  67.  
  68. <div class="row buttons">
  69. <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
  70. </div>
  71.  
  72. <?php $this->endWidget(); ?>
  73.  
  74. </div><!-- form -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement