MyCurlFile.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace WeChat\Contracts;
  3. /**
  4. * 自定义CURL文件类
  5. * Class MyCurlFile
  6. * @package WeChat\Contracts
  7. */
  8. class MyCurlFile extends \stdClass
  9. {
  10. /**
  11. * 当前数据类型
  12. * @var string
  13. */
  14. public $datatype = 'MY_CURL_FILE';
  15. /**
  16. * MyCurlFile constructor.
  17. * @param string|array $filename
  18. * @param string $mimetype
  19. * @param string $postname
  20. * @throws \WeChat\Exceptions\LocalCacheException
  21. */
  22. public function __construct($filename, $mimetype = '', $postname = '')
  23. {
  24. if (is_array($filename)) {
  25. foreach ($filename as $k => $v) $this->{$k} = $v;
  26. } else {
  27. $this->mimetype = $mimetype;
  28. $this->postname = $postname;
  29. $this->extension = pathinfo($filename, PATHINFO_EXTENSION);
  30. if (empty($this->extension)) $this->extension = 'tmp';
  31. if (empty($this->mimetype)) $this->mimetype = Tools::getExtMine($this->extension);
  32. if (empty($this->postname)) $this->postname = pathinfo($filename, PATHINFO_BASENAME);
  33. $this->content = base64_encode(file_get_contents($filename));
  34. $this->tempname = md5($this->content) . ".{$this->extension}";
  35. }
  36. }
  37. /**
  38. * 获取文件信息
  39. * @return \CURLFile|string
  40. * @throws \WeChat\Exceptions\LocalCacheException
  41. */
  42. public function get()
  43. {
  44. $this->filename = Tools::pushFile($this->tempname, base64_decode($this->content));
  45. if (class_exists('CURLFile')) {
  46. return new \CURLFile($this->filename, $this->mimetype, $this->postname);
  47. }
  48. return "@{$this->tempname};filename={$this->postname};type={$this->mimetype}";
  49. }
  50. /**
  51. * 类销毁处理
  52. */
  53. public function __destruct()
  54. {
  55. // Tools::delCache($this->tempname);
  56. }
  57. }