File Downloads
Sometimes you want to send files to the browser for download instead of displaying them.
NixPHP gives you full control to stream files using a custom response.
Downloading a File
You can manually create a response that forces a file download:
use function NixPHP\response;
$filePath = BASE_PATH . '/storage/files/example.pdf';
$fileName = 'example.pdf';
$response = response(file_get_contents($filePath))
->withHeader('Content-Type', 'application/octet-stream')
->withHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"');
return $response;
Content-Typetells the browser this is a generic file download.Content-Disposition: attachmentforces the download dialog.- The file is loaded into the response body.
Example: Download Controller
namespace App\Controllers;
use function NixPHP\response;
class FileController
{
public function download($filename)
{
$path = BASE_PATH . '/storage/files/' . basename($filename);
if (!file_exists($path)) {
abort(404, 'File not found.');
}
return response(file_get_contents($path))
->withHeader('Content-Type', 'application/octet-stream')
->withHeader('Content-Disposition', 'attachment; filename="' . basename($filename) . '"');
}
}
- Always use
basename()to prevent directory traversal attacks. - Always check if the file actually exists before sending it.
Notes
- For large files, you may want to implement streaming to avoid memory issues.
- You can adjust
Content-Typebased on the file type if needed (e.g.,application/pdffor PDFs).
Example for PDF:
return response(file_get_contents($pdfPath))
->withHeader('Content-Type', 'application/pdf')
->withHeader('Content-Disposition', 'attachment; filename="document.pdf"');
Summary
- Use
response(file_get_contents($path))to build file download responses. - Always set proper headers (
Content-Type,Content-Disposition). - Always sanitize filenames to prevent security issues.