아파치 X-Sendfile

백성현·2021년 7월 27일

X-Sendfile

설치

apt install libapache2-mod-xsendfile

설정

a2enmod xsendfile

mkdir -p /data/conetnt/example.com

cd /etc/apache2/sites-available
vi example.com.conf
###
<VirtualHost ...>
    ...
    XsendFile on
    XSendFilePath /data/content/example.com
</VirtualHost>
###

systemctl restart apache2

사용 예)

CodeIgniter 4.1.x

$localFilePath = '/data/content/example.com/example.jpg';

try {
    $file = new \CodeIgniter\Files\File($localFilePath, true);
    
    $this->response->setStatusCode(200);
    $this->response->setContentType($file->getMimeType());
    $this->response->setHeader('Content-Disposition', 'attachment; filename="' . $file->getRandomName() .  '"');
    $this->response->setHeader('X-Sendfile', $file->getRealPath());
} catch (\Exception $e) {
    $this->response->setStatusCode(404);
}

0개의 댓글