YiiHaa Webdevelopment blog about Yii-framework and more


Serving (large) files

Ever served large files with PHP? I bet you loved that! Do not worry, I have good news for you! There is a solution (or improvement, whatever you want to call it) to the old way of serving files, which used to look sort of like this:

<?php // Old method, which you shouldn't like anymore!
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="myFile"');
readfile(dirname(__FILE__) . '/myFile');

This guy "Nils Maier" created a nice module for Apache called mod_xsendfile. This module allows you to send a specific header to Apache via PHP, and it's really easy to implement:

<?php // 'New' and improved method!
header('X-SendFile: ' . dirname(__FILE__) . '/myFile');
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="myFile"');

Using this module has a couple of advantages when serving files:

  • you can control file-transfers with PHP, without actually serving using PHP
  • sets correct cache headers such as Etag and If-Modified-Since as if the file was statically served
  • processes cache headers such as If-None-Match or If-Modified-Since
  • and more..

There is just one downside to this, you need to install the module into Apache and enable it. Good thing installing it is a breeze!

wget https://tn123.org/mod_xsendfile/mod_xsendfile.c -O mod_xsendfile.c
apxs -cia mod_xsendfile.c
# Depending on your system, restart Apache
/etc/init.d/httpd restart

Now enable XSendFile in your .htaccess (or Apache config):

<IfModule mod_xsendfile.c>
  XSendFile on

Now you are set up and ready to upload the hell out of your server, without leaning on PHP, enjoy!

Posted by Klaas

Comments (2) Trackbacks (1)
  1. “Which was the point, preciselyM Nor, for that matter, did Hoffman explain how this “consensus” developed miraculously in the single day that had passed since the election B The blond, leonine singer-guitarist came down from his room in standard gear black T-shirt, black jeans, black boots all psyched up to order his first beer of the evening, when the maitre d’ informed him with frosty politeness that club rules require gentlemen to wear tiesnot only for her Best Leading Actress win,Asics Gel-K

  2. “But,KD 9 Royal Blue, dig, I just can’t do that And it was great though if you think about it, here’s us doing a song, and it was really a girls’ song He said, ‘I want you back in the skirts and saddle shoes,Nike Kyrie 2 USA, and out of One-Eyed Jacks!’”Whose Twin Peaks Are They, Anyway? “On the set of the pilot,” Sherilyn says, trying to clear up one mystery, “the makeup artist always called me Miss Twin Peaks Or rather, I was and he came in to take over Her previous release,Wmns Dunk Sky High,

Leave a comment