You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. namespace common\helpers;
  8. use yii\web\UploadedFile;
  9. class Upload {
  10. public static function uploadFile($model, $champs, $filename_old = '') {
  11. $file = UploadedFile::getInstance($model, $champs);
  12. if ($file) {
  13. $file_name = $file->baseName . '-' . uniqid() . '.' . $file->extension;
  14. $file->saveAs('../../frontend/web/uploads/' . $file_name);
  15. $model->$champs = $file_name;
  16. } else {
  17. $model->$champs = $filename_old;
  18. }
  19. $model->save();
  20. }
  21. }