PHP rest api how to return a file or download

  api, download, mysql, php, rest

I have an rest api and the endpoint is getfilestart.php. What it does is that it searches for the filename in the database using PDO. How can I get the file and return it as a file or automatically download it using postman or in the browser? I tried several methods but I cannot get the file. The directory is in ../files/

Here is the function for download

    public function downloadFile($param){
        $base_url = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'];
     
        $q = "SELECT * FROM data_file WHERE module_id = ". $param;
        $stmt = $this->conn->prepare($q);
        $stmt->execute();

        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        $filename = $row['file_name'];
        $request_url = $base_url .'/mtc_project_server/files/'.$filename;
    }

If i dump the request_url its http://localhost/mtc_project_server/files/mir4.PNG which is the actual file. How can i download it using rest? I tried doing cURL and file_get_contents and still not working. Thanks in advance

Source: Ask PHP

LEAVE A COMMENT