I am trying to download my uploaded files(image) by id from the database(phpmyadmin). but there's an error said that variable $id is undefined. this is the code i'm using, i got this code from some tutorial website.
public function download()
{
$berkas = new BerkasModel();
$data = $berkas->find($id);
return $this->response->download('uploads/berkas/' . $data->berkas, null);
}
how can i fix this so i can download the uploaded files?
You need to pass the variable $id
to the function.
Specific implementation depends on your setup. Looking at the small code fragment you posted (and the absence of any request variable extraction), pass it as function parameter:
public function download($id)
.
Furthermore you need to adapt your routes.php
and the calling <a>
to reflect this change:
routes.php
:
$routes->get('download/(:segment)', [CLASSNAME::class, 'download']);
<a href>
:
<a href="YOUR_URL/download/<?= esc($id) ?>">Download</a>