skip to Main Content

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?

2

Answers


  1. 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.phpand 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>
    
    Login or Signup to reply.
  2. You need $id in function download()

    public function download($id)
    {
        $berkas = new BerkasModel();
        $data = $berkas->find($id);
        return $this->response->download('uploads/berkas/' . $data->berkas, null);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search