phplaravellaravel-5php-7lumen

Laravel/Lumen file response


I need to stream file content (such as images and other mime types) from a Lumen resource server to a Laravel client server. I know in Laravel I can use:

$headers = ['Content-Type' => 'image/png']; 
$path = storage_path('/mnt/somestorage/example.png')
return response()->file($path, $headers);

However, the file method is absent in Laravel\Lumen\Http\ResponseFactory.

Any suggestions are very welcome.


Solution

  • In Lumen you can use Symfony's BinaryFileResponse.

    use Symfony\Component\HttpFoundation\BinaryFileResponse
    
    $type = 'image/png';
    $headers = ['Content-Type' => $type];
    $path = '/path/to/you/your/file.png';
    
    $response = new BinaryFileResponse($path, 200 , $headers);
    
    return $response;
    

    You can find the documentation here.