From f4c08f4c81cce853b5433c41ead1caa795f7ddad Mon Sep 17 00:00:00 2001 From: Anyon Date: Wed, 5 Dec 2018 15:25:42 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E5=A2=9E=E5=8A=A0=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89CURLFILE=E7=B1=BB=EF=BC=88=E6=96=B9=E4=BE=BF?= =?UTF-8?q?=E8=B7=A8=E6=9C=8D=E5=8A=A1=E5=99=A8=E6=96=87=E4=BB=B6=E4=BC=A0?= =?UTF-8?q?=E8=BE=93=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WeChat/Contracts/MyCurlFile.php | 55 +++++++++++++++++++++++++++++++++ WeChat/Contracts/Tools.php | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 WeChat/Contracts/MyCurlFile.php diff --git a/WeChat/Contracts/MyCurlFile.php b/WeChat/Contracts/MyCurlFile.php new file mode 100644 index 0000000..291c27e --- /dev/null +++ b/WeChat/Contracts/MyCurlFile.php @@ -0,0 +1,55 @@ +mimetype = $mimetype; + $this->postname = $postname; + $this->content = base64_encode(file_get_contents($filename)); + $this->extension = pathinfo($filename, PATHINFO_EXTENSION); + if (empty($this->extension)) $this->extension = 'tmp'; + if (empty($this->mimetype)) $this->mimetype = Tools::getExtMine($this->extension); + if (empty($this->postname)) $this->postname = pathinfo($filename, PATHINFO_BASENAME); + } + + /** + * 获取文件信息 + * @return \CURLFile|string + * @throws \WeChat\Exceptions\LocalCacheException + */ + public function get() + { + $this->tempname = rand(100000, 999999) . ".{$this->extension}"; + $this->filename = Tools::pushFile($this->tempname, base64_decode($this->content)); + if (class_exists('CURLFile')) { + return new \CURLFile($this->filename, $this->mimetype, $this->postname); + } + return "@{$this->tempname};filename={$this->postname};type={$this->mimetype}"; + } + + /** + * 类销毁处理 + */ + public function __destruct() + { + Tools::delCache($this->tempname); + } + +} \ No newline at end of file diff --git a/WeChat/Contracts/Tools.php b/WeChat/Contracts/Tools.php index 40e2371..1037836 100644 --- a/WeChat/Contracts/Tools.php +++ b/WeChat/Contracts/Tools.php @@ -50,7 +50,7 @@ class Tools /** * 根据文件后缀获取文件MINE - * @param array $ext 文件后缀 + * @param string|array $ext 文件后缀 * @param array $mine 文件后缀MINE信息 * @return string * @throws LocalCacheException