Skip to content

Instantly share code, notes, and snippets.

@haampie
Created August 19, 2016 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haampie/ac8bdf7ec33e4b658d9199a895dcec30 to your computer and use it in GitHub Desktop.
Save haampie/ac8bdf7ec33e4b658d9199a895dcec30 to your computer and use it in GitHub Desktop.
File contents and memory
<?php
$app->get('/', function () {
// Write some data to a temp file
$stream = fopen('php://temp', 'w+');
for ($i = 0; $i != 100000; $i++) {
fwrite($stream, str_repeat(strval($i), 100) . PHP_EOL);
}
// Go to the start
rewind($stream);
return response()->json([
'file' => base64_encode(stream_get_contents($stream))
]);
});
<?php
use Symfony\Component\HttpFoundation\StreamedResponse;
$app->get('/', function () {
// Write some data to a temp file
$stream = fopen('php://temp', 'w+');
for ($i = 0; $i != 100000; $i++) {
fwrite($stream, str_repeat(strval($i), 100) . PHP_EOL);
}
// Go to the start
rewind($stream);
return new StreamedResponse(function () use ($stream) {
fpassthru($stream);
}, 200, [
'Content-Type' => 'text/plain',
'Content-disposition' => 'attachment; filename="example.txt"',
]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment