download()
public function download(string $file_path, bool $as_attachment = true): void
Description
Initiates a file download or displays a file inline. This method handles header preparation, including Content-Type and Content-Disposition.
This method calls exit after sending the file to prevent additional output.
Parameters
| Parameter | Type | Description |
| $file_path |
string |
The path to the file. |
| $as_attachment |
bool |
(optional) Force download (true) or display inline (false). Default is true. |
Example #1
The code sample below demonstrates how to allow users to download their invoice PDFs.
public function download_invoice(): void {
$invoice_id = segment(3, 'int');
$user_id = segment(4, 'int');
// Get the invoice record
$invoice = $this->db->get_where($invoice_id, 'invoices');
if ($invoice === false || $invoice->user_id !== $user_id) {
redirect('invoices/not_found');
}
// Build the file path
$file_path = 'modules/invoices/documents/' . $invoice->filename;
// Check if file exists before attempting download
if ($this->file->exists($file_path)) {
// Trigger download
$this->file->download($file_path);
// Note: Code after this line will not execute due to exit
} else {
redirect('invoices/not_found');
}
}
Method Behavior: The download() method calls exit after sending the file, which terminates script execution. This means any code placed after the download() call will not run. There's no need to add a return statement or redirect after calling this method.
Example #2
The example above shows how to display a PDF inline in the browser rather than forcing a download.
public function view_contract(): void {
$contract_id = segment(3, 'int');
// Get the contract record
$contract = $this->db->get_where($contract_id, 'contracts');
if ($contract === false) {
redirect('contracts/not_found');
}
// Build the file path
$file_path = 'modules/contracts/storage/' . $contract->filename;
// Check if file exists
if ($this->file->exists($file_path)) {
// Display in browser instead of downloading
$this->file->download($file_path, false);
} else {
redirect('contracts/not_found');
}
}
Inline Display vs Download: Setting the second parameter to false causes the file to be displayed inline in the browser (if the browser supports the file type). This is useful for PDFs, images, and text files that users may want to preview before downloading. Setting it to true (the default) forces the browser to prompt a download dialog.
Example #3
The example above demonstrates how to implement a secure download system that logs file access.
public function secure_download(): void {
$file_id = segment(3, 'int');
$user_id = $_SESSION['user_id'] ?? 0;
// Verify user is logged in
if ($user_id === 0) {
redirect('login');
}
// Get file record and check permissions
$file = $this->db->get_where($file_id, 'secure_files');
if ($file === false || $file->owner_id !== $user_id) {
redirect('files/unauthorized');
}
$file_path = 'modules/secure_files/storage/' . $file->filename;
if ($this->file->exists($file_path)) {
// Log the download
$log_data = [
'file_id' => $file_id,
'user_id' => $user_id,
'downloaded_at' => date('Y-m-d H:i:s')
];
$this->db->insert($log_data, 'download_logs');
// Initiate download
$this->file->download($file_path);
} else {
redirect('files/not_found');
}
}