workerman请求静态资源如何增加头信息

sxxzwzf
$http_worker->onMessage = static function ($connection, $request) {
    // 静态资源目录
    $staticDir = 'G:/sms/sms/laraval/resources';
    // 请求的文件路径
    $filePath = $staticDir . $request['server']['REQUEST_URI'];
    // 检查文件是否存在
    if(is_file($filePath)) {
        // 直接返回文件内容
        // ======发送http头======
        $file_size = filesize($filePath);
        $header = "HTTP/1.1 200 OK\r\n";

        $header .= "Content-Type: ". mime_content_type($filePath) . "\n";
        $header .= "Connection: keep-alive\r\n";
        $header .= "Content-Length: $file_size\r\n\r\n";
        $connection->send($header, true);
        $fileContent  = file_get_contents($filePath);

        $connection->send($fileContent);
    } else {
        $connection->send(run());
    }
};

这样写请求静态资源header 没有文件类型,客户端打开的图片是图片内容,应该怎样修改
截图

84 1 0
1个回答

walkor
$header .= "Content-Type: ". mime_content_type($filePath) . "\n";

改成

$header .= "Content-Type: ". mime_content_type($filePath) . "\r\n";

试下

推荐用workerman4.x或者后续更高版本,走http协议,不要自己拼http头

  • 暂无评论
×
🔝