国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
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.

MyCurlFile.php 1.9KB

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