In your Yii2 controller it is quite common practice to return the stream of a file to the browser for your user to download.
However, it’s not so well documented you can set any stream reference as the content of your response.
In this scenario we want to have a file behind yii2 authentication that only some users can view. We could use the
1 | sendFile |
function to send a file stream to the browser. This will prompt the user to download the file. In this situation we just want the users to view the files.
This is where the
1 | stream |
property of the Yii2 response comes in. With this bit of code it will send the stream as the content of your response. The below code is a snippet will send a PNG file reference to the browser without using the
1 | file_get_contents |
function where we will be loading the full content of the file into memory rather than reading the file line by line.
1
2
3
4
5
6 public function actionViewFile(): void
{
Yii::$app->response->format = Yii::$app->response::FORMAT_RAW;
Yii::$app->response->headers->set('Content-Type', 'image/png');
Yii::$app->response->stream = fopen('/path/to/my.png', 'rb');
}
The stream property of the response can also be the stream from the
1 | flysystem |
fs adapter, so we can stream our files from s3 straight to the browser. Below is a snippet using yii2 flysystem to connect to an aws s3 bucket.
1
2
3
4
5
6 public function actionViewFile(): void
{
Yii::$app->response->format = Yii::$app->response::FORMAT_RAW;
Yii::$app->response->headers->set('Content-Type', 'image/png');
Yii::$app->response->stream = Yii::$app->fs->readStream('my.png');
}
More reads
Start a new development project on the right foot
Multiple times in Web Development you will go through the process of creating a new repository for a new application or project that you are tasked to build. We think it is really important to get the order of tasks correct at the start of a new application…
Accessibility Web Design Rules – Our upgraded website for NHS
Things we learnt by creating highly accessible website designs for the NHS & university sectors. Recent pitches and web briefs have put accessibility high on their list of needs, which is a great sign. However accessibility, just like GDPR, needs to be taken in context…
Divi alt tags done right (in functions.php)
DIVI for Wordpress. Load Alt tags in Divi directly from the Media Library, pull alt tags in automatically even when these are added or changed from the Wordpress Media Library.