Deleting Files
The delete flow follows the same confirm-then-delete pattern from the Basic CRUD chapter. The key difference is that our delete operation removes the file from two places: the database record and the physical file on disk.
The Confirm Delete Method
public function confirm_delete(): void {
$this->gatekeeper();
$update_id = segment(3, 'int');
$data = $this->model->get_file_by_id($update_id);
if ($data === false) {
$this->not_found();
return;
}
$data['headline'] = 'Delete File';
$data['cancel_url'] = BASE_URL
. 'file_manager/show/'
. $update_id;
$data['form_location'] = str_replace(
'/confirm_delete',
'/submit_confirm_delete',
current_url()
);
$data['update_id'] = $update_id;
$data['view_module'] = 'file_manager';
$data['view_file'] = 'confirm_delete';
$this->templates->admin($data);
}The confirmation page shows the document name and filename, then asks the user to confirm. A warning message emphasizes that the action is irreversible - the file will be removed from both the database and the filesystem.
The Confirm Delete View
<div class="container">
<h1><?= out($headline) ?></h1>
<p>
Are you sure you want to permanently
delete the following file?
</p>
<table class="table">
<tr>
<th style="width: 200px;">
Document Name
</th>
<td><?= out($doc_name) ?></td>
</tr>
<tr>
<th>File Name</th>
<td>
<code>
<?= out($file_name) ?>
</code>
</td>
</tr>
</table>
<div class="alert alert-danger">
<strong>Warning:</strong>
This action is irreversible.
The file will be permanently removed
from both the database and the server
filesystem.
</div>
<?= form_open($form_location) ?>
<div class="form-group">
<button type="submit"
class="btn btn-danger">
<i class="fa-solid fa-trash"></i>
Yes, Delete This File
</button>
<a href="<?= out($cancel_url) ?>"
class="btn btn-default">
Cancel
</a>
</div>
<?= form_close() ?>
</div>The Submission Method
public function submit_confirm_delete(): void {
$this->gatekeeper();
$update_id = segment(3, 'int');
$data = $this->model->get_file_by_id($update_id);
if ($data === false) {
$this->not_found();
return;
}
// Delete from filesystem first
$file_path = $data['file_path'];
if (file_exists($file_path)) {
$this->file->delete($file_path);
}
// Then delete from database
$this->model->delete_file($update_id);
set_flashdata(
'The file record was successfully deleted.'
);
redirect('file_manager/manage');
}Filesystem First, Database Second
The method deletes from the filesystem before deleting from the database. This is intentional:
- If the filesystem delete fails (e.g., permission error), the database record still exists and the admin can investigate.
-
If we deleted the database record first and then the filesystem delete failed, we would lose the record's
file_pathand have no way to locate the orphaned file.
The file_exists() check before deletion is also deliberate. It handles the case where the physical file was already removed (by an admin, a cleanup script, etc.) - the method simply skips the filesystem delete and removes the database record cleanly.
Irreversibility
Unlike the countries module where a deleted record can be re-created by submitting a form, a deleted file cannot be recovered. The actual bytes are gone. This is why the confirmation page displays a prominent warning and why the two-step confirm-then-delete pattern is particularly important for file management.
We're continually improving the Trongate documentation. If anything is incorrect, unclear, incomplete, or could be better, we'd genuinely appreciate your input.
Share your thoughts in the Documentation Feedback.