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; }
Advertisement
Answer
I searched for hours but couldn’t find an answer so I then decided to take a totally different approach which works fine:
$filename = basename($filepath); set_time_limit(0); ini_set('memory_limit', '25M'); while (ob_get_level()) ob_end_clean(); header('Content-Type: audio/mpeg'); header('Content-Disposition: attachment; filename=' . basename($filepath)); header('Cache-Control: no-cache'); header("Content-Transfer-Encoding: chunked"); ob_flush(); flush(); readfile($filepath); exit;