Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

29 lines
724B

  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. }