Passthru() works with exit(), not with Yii::app()->end();

Hey,

I’m using an Yii2 basic web application, which used passthru() to execute an command on system in order to pass the binary output to the browsers client (download).

So I have an controller/action called by an ajax-request, which does the following:

            header("Cache-Control: private");
            header("Content-Type: application/octet-stream");
            header("Content-Disposition: attachment; filename=download.jpg");
            passthru ('/bin/cat /PATH/pic.jpg');

(btw: this is pseudo code to show the problem. )

Taking a look at the downloaded file, u will find out, that the download.jpg is not equal to pic.jpg.
Sometimes. In some cases, there are some bytes missing in the download.jpg, which results in an picture, which is not readable by some viewers.

If I close this action explicit by using exit() at the end --> everything works. The download.jpg has always the same md5 checksume like the pic.jpg.
If I use Yii::app()->end(); instead of exit() at the end, I have the same problem like described above: sometimes some missing bytes in the download.jpg

Any idea why this happens?

Idk why that happens but there alternatives to this way of delivering a file. See if you can use X-Sendfile instead.

1 Like