以下是在論壇看到的大文件發(fā)送例子
<?php
use Workerman\Worker;
require_once './Workerman/Autoloader.php';
$worker = new Worker('http://0.0.0.0:4236');
$worker->onMessage = function($connection, $data)
{
if($_SERVER == '/favicon.ico')
{
return $connection->send("HTTP/1.0 404 Not Found\r\nContent-Length: 0\r\n\r\n", true);
}
// 這里發(fā)送的是一個大的pdf文件,如果是其它格式的文件,請修改下面代碼中http頭
send_file($connection, "/your/path/xxx.pdf");
};
function send_file($connection, $file_name)
{
if(!is_file($file_name))
{
$connection->send("HTTP/1.0 404 File Not Found\r\nContent-Length: 18\r\n\r\n404 File Not Found", true);
return;
}
// ======發(fā)送http頭======
$file_size = filesize($file_name);
$header = "HTTP/1.1 200 OK\r\n";
// 這里寫的Content-Type是pdf,如果不是pdf文件請修改Content-Type的值
// mime對應關系參見 https://github.com/walkor/Workerman/blob/master/Protocols/Http/mime.types#L30
$header .= "Content-Type: application/pdf\r\n";
$header .= "Connection: keep-alive\r\n";
$header .= "Content-Length: $file_size\r\n\r\n";
$connection->send($header, true);
// ======分段發(fā)送文件內容=======
$connection->fileHandler = fopen($file_name, 'r');
$do_write = function()use($connection)
{
// 對應客戶端的連接發(fā)送緩沖區(qū)未滿時
while(empty($connection->bufferFull))
{
// 從磁盤讀取文件
$buffer = fread($connection->fileHandler, 8192);
// 讀不到數據說明文件讀到末尾了
if($buffer === '' || $buffer === false)
{
return;
}
$connection->send($buffer, true);
}
};
// 發(fā)生連接發(fā)送緩沖區(qū)滿事件時設置一個標記bufferFull
$connection->onBufferFull = function($connection)
{
// 賦值一個bufferFull臨時變量給鏈接對象,標記發(fā)送緩沖區(qū)滿,暫停do_write發(fā)送
$connection->bufferFull = true;
};
// 當發(fā)送緩沖區(qū)數據發(fā)送完畢時觸發(fā)
$connection->onBufferDrain = function($connection)use($do_write)
{
$connection->bufferFull = false;
$do_write();
};
// 執(zhí)行發(fā)送
$do_write();
}
Worker::runAll();
請問是不是Nginx配置原因導致的