Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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