Yii2 already has a built-in function for sending files . Thus, you do not need to set the response format, and the type of content will be detected automatically (you can cancel it if you want):
function actionDownload() { $imgFullPath = 'picture.jpg'; return Yii::$app->response->sendFile($imgFullPath); }
..
If the file is temporarily created for the current upload action, you can use the AFTER_SEND event to delete the file:
function actionDownload() { $imgFullPath = 'picture.jpg'; return Yii::$app->response ->sendFile($imgFullPath) ->on(\yii\web\Response::EVENT_AFTER_SEND, function($event) { unlink($event->data); }, $imgFullPath); }
source share