I use this function to download MP3 files from my server; The files are a few megs large, anywhere between 1 and 10 megs. It works very well however it stops working after a while, it then permanently raises an error (catch section) whenever we use the function, this until I restart the web server. As soon as I restart it, it resumes behaving correctly. The web server is OpenLiteSpeed 1.4.
Kindly advise if I am using the BinaryFileResponse function correctly or not?
function getFile($user_id, $filename)
{
if (empty($filename)) {
throw new \InvalidArgumentException('Please provide a valid filename.');
} else {
$filepath = "../files/$filename";
BinaryFileResponse::trustXSendfileTypeHeader();
try {
$response = new BinaryFileResponse($filepath);
$mimeTypes = new MimeTypes();
$mimeType = $mimeTypes->guessMimeType($filepath);
$response->headers->set('Content-Type', $mimeType);
$response->setAutoEtag();
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$filename
);
} catch (FileNotFoundException $exception) {
$response = new JsonResponse(
['Status' => 'Error', 'Message' => DEBUG ? $exception->getMessage() : 'File not found.'],
Response::HTTP_NOT_FOUND
);
}
}
return $response;
}