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

如何向http客戶端發(fā)送大文件(幾百兆 1個(gè)多G)?

lllpaw

如何向http客戶發(fā)送幾百兆 1G的大文件?PHP讀取大文件一般采用每次讀取一塊字符串,至到文件結(jié)束。問題是向?yàn)g覽器發(fā)送第一塊后http協(xié)議就會(huì)關(guān)閉會(huì)話,后續(xù)塊無法發(fā)送。
?
比如通過瀏覽器下載文件這種應(yīng)用。

5927 1 2
1個(gè)回答

walkor 打賞

這有個(gè)例子,摘自workerman/WebServer.php里的一個(gè)發(fā)送文件的方法。

function sendFile($connection, $file_path)
{
? ??_// Check 304.
? ??_$info =?_stat_($file_path);
? ? $modified_time = $info ??_date_('D,?d M Y H:i:s', $info) .?' '?.?_date_default_timezone_get_() :?'';
? ??if?(!empty($_SERVER) && $info) {
? ? ? ??_// Http 304.
? ? ? ??_if?($modified_time === $_SERVER) {
? ? ? ? ? ??_// 304
? ? ? ? ? ??_Http::_header_('HTTP/1.1 304 Not Modified');
? ? ? ? ? ??_// Send nothing but http headers..
? ? ? ? ? ??_$connection->close('');
? ? ? ? ? ??return;
? ? ? ? }
? ? }

? ??_// Http header.
? ??_if?($modified_time) {
? ? ? ? $modified_time =?"Last-Modified:?$modified_time\r\n";
? ? }
? ? $file_size =?_filesize_($file_path);
? ? $file_info =?_pathinfo_($file_path);
? ? $extension =?isset($file_info) ? $file_info :?'';
? ? $file_name =?isset($file_info) ? $file_info :?'';
? ? $header =?"HTTP/1.1 200 OK\r\n";
? ? //if?(isset(self::_$mimeTypeMap_)) {
? ? ? ? //$header .=?"Content-Type: "?.?self::_$mimeTypeMap_ .?"\r\n";
? ? //}?else?{
? ? ? ? $header .=?"Content-Type: application/octet-stream\r\n";
? ? ? ? $header .=?"Content-Disposition: attachment;?filename=\"$file_name\"\r\n";
? ? //}
? ? $header .=?"Connection: keep-alive\r\n";
? ? $header .= $modified_time;
? ? $header .=?"Content-Length:?$file_size\r\n\r\n";
? ? $trunk_limit_size = 1024*1024;
? ??if?($file_size < $trunk_limit_size) {
? ? ? ??return?$connection->send($header._file_get_contents_($file_path),?true);
? ? }
? ? $connection->send($header,?true);

? ??_// Read file content from disk piece by piece and send to client.
? ??_$connection->fileHandler?=?_fopen_($file_path,?'r');
? ? $do_write =?function()use($connection)
? ? {
? ? ? ??_// Send buffer not full.
? ? ? ??_while(empty($connection->bufferFull))
? ? ? ? {
? ? ? ? ? ??_// Read from disk.
? ? ? ? ? ??_$buffer =?_fread_($connection->fileHandler, 8192);
? ? ? ? ? ??_// Read eof.
? ? ? ? ? ??_if($buffer ===?''?|| $buffer ===?false)
? ? ? ? ? ? {
? ? ? ? ? ? ? ??return;
? ? ? ? ? ? }
? ? ? ? ? ? $connection->send($buffer,?true);
? ? ? ? }
? ? };
? ??_// Send buffer full.
? ??_$connection->onBufferFull?=?function($connection)
? ? {
? ? ? ? $connection->bufferFull?=?true;
? ? };
? ??_// Send buffer drain.
? ??_$connection->onBufferDrain?=?function($connection)use($do_write)
? ? {
? ? ? ? $connection->bufferFull?=?false;
? ? ? ? $do_write();
? ? };
? ? $do_write();
}
年代過于久遠(yuǎn),無法發(fā)表回答
??