Method
ClientRequest::sendFile
Write a file, or part of one, as the body: commits the head, then streams it as write() does, under the same framing rules. No content-length is computed; send one with the head.
Description#
public ClientRequest::sendFile(
$stream,
?int $offset = null,
?int $length = null
): voidParameters#
- $stream
- A readable stream, such as fopen()'s
- $offset ?int = null
- Where to start reading, null: where the stream is
- $length ?int = null
- How many bytes, null: to the end
Examples#
use Swerve\ClientRequest;
use Swerve\RequestHandler;
$app = new RequestHandler(function (ClientRequest $request) {
$file = fopen('php://temp', 'w+');
fwrite($file, "0123456789abcdefghij");
// No content-length is computed: give one with the head, or the body is chunked
$request->sendResponseHeaders(200, ['content-type' => 'text/plain', 'content-length' => '6']);
$request->sendFile($file, 10, 6); // 6 bytes from offset 10
fclose($file);
});
if (debug_backtrace()) {
return $app; // swerve loads this file: it is the application's swerve.php
}
// Run directly: serve the application with swerve, and send it requests
require __DIR__ . '/../serve.php';
serve(__FILE__, ["GET /file HTTP/1.1\r\nHost: example\r\n\r\n"]);Output
HTTP/1.1 200 OK content-type: text/plain Server: Swerve Content-Length: 6 Connection: close abcdef