UploadedFile.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: NODELOG
  5. * DateTime: 2017/7/12 15:33
  6. * Description:
  7. */
  8. namespace common\modules\attachment\components;
  9. use common\helpers\StringHelper;
  10. use yii\helpers\ArrayHelper;
  11. /**
  12. * Class UploadedFile
  13. * @package common\modules\attachment\components
  14. * @property string $hashName
  15. */
  16. class UploadedFile extends \yii\web\UploadedFile
  17. {
  18. public function getHashName($path = null)
  19. {
  20. if ($path) {
  21. $path = rtrim($path, '/').'/';
  22. }
  23. $hash = \Yii::$app->security->generateRandomString(40);
  24. return $path.$hash.'.'.$this->getExtension();
  25. }
  26. public function store($path, $options = [])
  27. {
  28. return $this->storeAs($path, $this->getHashName(), $this->parseOptions($options));
  29. }
  30. public function storeAs($path, $name, $options = [])
  31. {
  32. $options = $this->parseOptions($options);
  33. $disk = ArrayHelper::remove($options, 'disk');
  34. return \Yii::$app->storage->disk($disk)->putFileAs(
  35. $path, $this, $name, $options
  36. );
  37. }
  38. protected function parseOptions($options)
  39. {
  40. if (is_string($options)) {
  41. $options = ['disk' => $options];
  42. }
  43. return $options;
  44. }
  45. }