Lumen Framework version: 5.3.2
PHP version : 7.0.9
OS : Ubuntu 16.04 (reproducible in Mac too)
Framework is throwing an error when using
return response()->download($file)->deleteFileAfterSend(true);
with any global middlewares.
It seems that the file gets deleted on this line:
https://github.com/laravel/lumen-framework/blob/v5.3.2/src/Concerns/RoutesRequests.php#L479
And then the error occurs due to this line:
https://github.com/laravel/lumen-framework/blob/v5.3.2/src/Concerns/RoutesRequests.php#L501
which calls Symfony\Component\HttpFoundation\BinaryFileResponse::prepare() and throws an error in this line:
https://github.com/symfony/symfony/blob/v3.1.7/src/Symfony/Component/HttpFoundation/BinaryFileResponse.php#L189
The error is:
RuntimeException: SplFileInfo::getSize(): stat failed for /../lumen-5.3/storage/test.csv in /../lumen-5.3/vendor/symfony/http-foundation/BinaryFileResponse.php:189
Stack trace:
#0 /../lumen-5.3/vendor/symfony/http-foundation/BinaryFileResponse.php(189): SplFileInfo->getSize()
#1 /../lumen-5.3/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(801): Symfony\Component\HttpFoundation\BinaryFileResponse->prepare(Object(Illuminate\Http\Request))
#2 /../lumen-5.3/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(501): Laravel\Lumen\Application->prepareResponse(Object(Symfony\Component\HttpFoundation\BinaryFileResponse))
#3 /../lumen-5.3/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(485): Laravel\Lumen\Application->callTerminableMiddleware(Object(Symfony\Component\HttpFoundation\BinaryFileResponse))
#4 /../lumen-5.3/public/index.php(28): Laravel\Lumen\Application->run()
#5 {main}
The file gets downloaded and deleted successfully, it's just that error becomes an issue when you have an error handling system in place.
You could replicate the issue by cloning my repository:
https://github.com/bmvzane/lumen-5.3
The error should appear in the lumen log file.
That's not a lumen's bug.
But to work around with this issue i did this:
<?php
...
$response = response()->download(storage_path('app/' . $zipperFilename));
register_shutdown_function('unlink', storage_path('app/' . $zipperFilename));
return $response;
I can reproduce this but at the very least I don't think this is an issue related to Lumen. It's probably best if you check with Symfony.
I also fount his stack overflow issue which might be a hint at tweaking your php.ini settings: https://stackoverflow.com/questions/24138189/runtimeexception-splfileinfogetsize-stat-failed-for-laravel-4-upload-ima
Most helpful comment
That's not a lumen's bug.
But to work around with this issue i did this: