getDir() . uniqid() . '.' . $ext; return $path; } /** * 保存临时文件 * * @param string $ext 文件扩展名 * @param string $fileString 文件内容 * @return string 存储路径 * @throws \Exception 存储失败时抛出异常 */ public function save($ext, $fileString) { $path = $this->getStorePath($ext); $res = $this->getDisk()->put($path, $fileString); if (!$res) { throw new \Exception("临时储存失败"); } return $path; } /** * 获取临时文件下载URL * * @param string $path 文件路径 * @return string 下载URL */ public function getDownUrl($path) { $path = config('app.name') . '_temp/' . $path; $res = $this->getDisk()->url($path); return $res; } /** * 获取临时文件下载响应 * * @param string $path 文件路径 * @return \Illuminate\Http\Response 下载响应 */ public function getDown($path) { $path = config('app.name') . '_temp/' . $path; $disk = $this->getDisk(); $res = $disk->read($path); $headers = []; $headers['Content-Type'] = $disk->mimeType($path); $headers['Content-Length'] = $disk->size($path); $resp = response('', 200, $headers)->setContent($res); return $resp; } /** * 路径转URL * * @param string $path 文件路径 * @return string URL */ public function path2url($path) { $new = substr($path, strpos($path, 'temp')); return $new; } /** * 获取本地临时文件路径 * * @return string 本地临时文件路径 */ public function getTempLocalFile() { $file = sys_get_temp_dir() . '/' . uniqid(); return $file; } }