I try to send a file to the user with this function
Yii::$app->response->sendFile($tmp_filename, 'test.RData');
Now I want the file to be deleted after sending. I know that there's an event handler for send
method in yii/web/Response
called EVENT_AFTER_SEND
I tried to access this handler with the following code:
Event::on(\yii\web\Response::className(), \yii\web\Response::EVENT_AFTER_SEND, function ($event) {
unlink($event->response->filename);
});
But my problem is
a) I'm not sure if this is the right way
b) how to access the filename inside the event
Any help is appreciated!
I had the same problem this week. The documentation says that we can use the $data
parameter to add any variable we want on this callback. Here is a example:
Yii::$app->response->sendFile('/path/of/my/temp/file')->on(\yii\web\Response::EVENT_AFTER_SEND, function($event) {
unlink($event->data);
}, '/path/of/my/temp/file');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With