In Yii2 how to save a file from the server?

Hello. There is a link called "upload to dbf". It leads to a controller which generates a dbf file and puts it in the archive. The files appear on the server in the web folder. Now how to download it? Need to return a reference?
I tried something like this, nothing came of it:
return Yii::getAlias('@app/web/'.$this->file_zip);
ps. I am new to yii.
March 19th 20 at 08:45
1 answer
March 19th 20 at 08:47
Solution
As an option
https://habr.com/ru/post/151795/

Here's another method for Yii2
<?php
public function actionDownload() {
 $path = \Yii::getAlias('@uploads') ;
 $file = $path . '/some-file.pdf';

 if (file_exists($file)) {
 return \Yii::$app->response->sendFile($file);
 } 
 throw new \Exception('File not found');
}
Thank you! Works!
file_force_download function($file) {
 if (file_exists($file)) {
 // reset the PHP output buffer, to avoid overflow of the memory allocated for the script
 // if this is not done the file will be read into memory completely!
 if (ob_get_level()) {
ob_end_clean();
}
 // force the browser to show the save file dialog box
 header('Content-Description: File Transfer');
 header('Content-Type: application/octet-stream');
 header('Content-Disposition: attachment; filename=' . basename($file));
 header('Content-Transfer-Encoding: binary');
 header('Expires: 0');
 header('Cache-Control: must-revalidate');
 header('Pragma: public');
 header('Content-Length:' . filesize($file));
 // read the file and send it to the user
readfile($file);
exit;
}
}
- Margarett commented on March 19th 20 at 08:50
@Margarett, only it is better to use the built-in framework decision, instead of writing their own bikes - Deonte_Jaskolski94 commented on March 19th 20 at 08:53

Find more questions by tags Yii