StreamController.php 817 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace demo\Controllers;
  3. use Silex\Application;
  4. use Symfony\Component\HttpFoundation\Response;
  5. /**
  6. * Class StreamController demos streaming of static files and custom content.
  7. *
  8. * @package demo\Controllers
  9. */
  10. class StreamController {
  11. public function customStream(Application $app) {
  12. $noise = base64_encode(random_bytes(2048));
  13. $stream = fopen('php://memory', 'r+');
  14. fwrite($stream, $noise);
  15. rewind($stream);
  16. $streamer = function () use($stream) {
  17. while (!feof($stream)) {
  18. echo fread($stream, 1024);
  19. ob_flush();
  20. flush();
  21. }
  22. fclose($stream);
  23. };
  24. return $app->stream($streamer, Response::HTTP_OK, ['Content-type' => 'text/plain']);
  25. }
  26. public function fileStream(Application $app) {
  27. return $app->sendFile('/etc/passwd');
  28. }
  29. }