Support the ongoing development of → Laravel Architecture
Last updated 2 years ago.

lkeyepax liked this thread


As Symfony HttpFoundation doc. says in the serving file paragraph:"if you are serving a static file, you can use a BinaryFileResponse"

The .mp3 files I need to serve are statics in the server and stored in "/storage/songs/" so I decided to use the BinaryFileResponse, and the method for serving .mp3 became:

use Symfony\Component\HttpFoundation\BinaryFileResponse;

public function getSong(Song $song) {
	$path = storage_path().$song->path.".mp3");
	$user = \Auth::user();
	if($user->activated_at) {
		$response = new BinaryFileResponse($path);
		return $response; 

The BinaryFileResponse automatically handle the requests and allow you to serve the file entirely (by making just one request with Http 200 code) or splitted for slower connection (more requests with Http 206 code and one final request with 200 code).

lkeyepax, muhammadalimuhammadjonov liked this reply


Sign in to participate in this thread!


Your banner here too?


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.