国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

webman 文件下載完成后怎么實現(xiàn)監(jiān)聽刪除原文件

kzhzjdyw888

這邊使用的是workerman內(nèi)置組件

    public function downloadExcel(Request $request)
    {
        try {
            $param = $request->all();
            $file  = ltrim($param['file_path'] ?? '', '/');

            // 路徑安全校驗
            $basePath = realpath(runtime_path());
            $realPath = realpath($basePath . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $file));
            if (!$realPath || strpos($realPath, $basePath) !== 0 || !is_file($realPath)) {
                throw new \RuntimeException('文件不存在或路徑非法');
            }

            // 統(tǒng)一使用UTF-8編碼
            $downloadName = $param['file'] ?? date('Y-m-d_His');
            $encodedName  = rawurlencode($downloadName);

            // 創(chuàng)建響應(yīng)
            $response = new BinaryFileResponse($realPath);
            $response->setContentDisposition(
                ResponseHeaderBag::DISPOSITION_ATTACHMENT,
                "filename*=UTF-8''{$encodedName}.xlsx"
            );

            $response->headers->add([
                'Cache-Control' => 'private, max-age=600',
                'Content-Type'  => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            ]);

            $response->headers->set('Content-Length', filesize($realPath));

            $response->deleteFileAfterSend(true);

            return $response;
        } catch (\Throwable $e) {
            return response()->json([
                'code' => 500,
                'msg'  => '下載失敗:' . $e->getMessage(),
            ], 500);
        }
    }

實際返回如下 就是沒辦法觸發(fā)下載

HTTP/1.0 200 OK Cache-Control: max-age=600, private Content-Disposition: attachment; filename="filename=UTF-8''_E5_AF_BC_E5_87_BA_20250523153125.xlsx.xlsx"; filename=utf-8''filename%2A%3DUTF-8%27%27%25E5%25AF%25BC%25E5%2587%25BA_20250523153125.xlsx.xlsx Content-Length: 13082 Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet Date: Fri, 23 May 2025 07:31:34 GMT Last-Modified: Fri, 23 May 2025 07:31:26 GMT

404 4 0
4個回答

shiroi

這種還是需要自己實現(xiàn),寫個watch

  • 暫無評論
晚安。

用延時隊列,下載幾分鐘之后刪除文件

  • 暫無評論
jack10082009

我用的workerman,單開一個worker,然后使用定時器定時清除。當(dāng)然這個定時器我讓他干了好多活,如果就一個活的話單開一個worker有點奢侈:)

  • 暫無評論
ab0029

開啟協(xié)程的話就很方便了,直接一個defer搞定

  • 暫無評論
??