yii\web\Response::xSendFile PHP Method

xSendFile() public method

X-Sendfile is a feature allowing a web application to redirect the request for a file to the webserver that in turn processes the request, this way eliminating the need to perform tasks like reading the file and sending it to the user. When dealing with a lot of files (or very big files) this can lead to a great increase in performance as the web application is allowed to terminate earlier while the webserver is handling the request. The request is sent to the server through a special non-standard HTTP-header. When the web server encounters the presence of such header it will discard all output and send the file specified by that header using web server internals including all optimizations like caching-headers. As this header directive is non-standard different directives exists for different web servers applications: - Apache: X-Sendfile - Lighttpd v1.4: X-LIGHTTPD-send-file - Lighttpd v1.5: X-Sendfile - Nginx: X-Accel-Redirect - Cherokee: X-Sendfile and X-Accel-Redirect So for this method to work the X-SENDFILE option/module should be enabled by the web server and a proper xHeader should be sent. **Note** This option allows to download files that are not under web folders, and even files that are otherwise protected (deny from all) like .htaccess. **Side effects** If this option is disabled by the web server, when this method is called a download configuration dialog will open but the downloaded file will have 0 bytes. **Known issues** There is a Bug with Internet Explorer 6, 7 and 8 when X-SENDFILE is used over an SSL connection, it will show an error message like this: "Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found.". You can work around this problem by removing the Pragma-header. **Example** php Yii::$app->response->xSendFile('/home/user/Pictures/picture1.jpg');
See also: sendFile()
public xSendFile ( string $filePath, string $attachmentName = null, array $options = [] )
$filePath string file name with full path
$attachmentName string file name shown to the user. If null, it will be determined from `$filePath`.
$options array additional options for sending the file. The following options are supported: - `mimeType`: the MIME type of the content. If not set, it will be guessed based on `$filePath` - `inline`: boolean, whether the browser should open the file within the browser window. Defaults to false, meaning a download dialog will pop up. - xHeader: string, the name of the x-sendfile header. Defaults to "X-Sendfile".
    public function xSendFile($filePath, $attachmentName = null, $options = [])
    {
        if ($attachmentName === null) {
            $attachmentName = basename($filePath);
        }
        if (isset($options['mimeType'])) {
            $mimeType = $options['mimeType'];
        } elseif (($mimeType = FileHelper::getMimeTypeByExtension($filePath)) === null) {
            $mimeType = 'application/octet-stream';
        }
        if (isset($options['xHeader'])) {
            $xHeader = $options['xHeader'];
        } else {
            $xHeader = 'X-Sendfile';
        }
        $disposition = empty($options['inline']) ? 'attachment' : 'inline';
        $this->getHeaders()->setDefault($xHeader, $filePath)->setDefault('Content-Type', $mimeType)->setDefault('Content-Disposition', $this->getDispositionHeaderValue($disposition, $attachmentName));
        $this->format = self::FORMAT_RAW;
        return $this;
    }