Created
August 19, 2016 16:12
-
-
Save haampie/ac8bdf7ec33e4b658d9199a895dcec30 to your computer and use it in GitHub Desktop.
File contents and memory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)) | |
]); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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