6

Basically, I would like to send a header X-Sendfile to the browser to send a file, but I don't want to call this if the X-Sendfile is not available or installed on the server. How can I check for this in PHP?? Or if this is impossible to check in PHP, than how to check that it is installed PERIOD? I would rather check for the existence of X-Sendfile in PHP, as it would be easier for me to do so, since this is part of a package that will run on other sites and servers as well... Perhaps if I just use it with the PHP header function, it will return something if not installed??

Thanks guys :)

Solomon Closson
  • 6,111
  • 14
  • 73
  • 115
  • For those who come here and are looking for checking through shell (as opposed to the OP who has asked checking through PHP) use this command: `[ $(apachectl -M | grep xsendfile_module | wc -l) -eq 1 ] && echo 'installed' || echo 'not-installed'`. If it's installed "installed" will be printed, and otherwise "not-installed" will be printed. – aderchox Nov 11 '21 at 15:40

4 Answers4

6

The APACHE module mod_xsendfile processes the X-Sendfile headers

To check if the APACHE module mod_xsendfile is available and installed on the server, you could use apache_get_modules() function.

You cannot just set the header and check if the module is installed or not.

verisimilitude
  • 5,077
  • 3
  • 30
  • 35
  • Save your "thanks" for someone else. I'm more than glad to have helped you out. – verisimilitude Sep 02 '12 at 07:10
  • 3
    Don't forget: the module might be loaded (listed in apache_get_modules(), but might not be On (it's even disabled by default). You cannot check this with PHP. – marcovtwout Mar 14 '13 at 11:05
1

To get a list of apache modules and see if x-sendfile is in the list you could use http://php.net/manual/en/function.apache-get-modules.php If it's not installed, the x-sendfile header will get to the browser if it is installed, the module will filter out the header.

rx80
  • 156
  • 4
0

Try this:

    if (in_array('mod_xsendfile', apache_get_modules())) {
        header("X-Sendfile: $file");
    } else {
        error_log("Warning! mod-xsendfile is NOT INSTALLED - sending file the old fashion way.....");
        header('Content-Length: ' . filesize($file) );
        print file_get_contents($file);
    }
msEmmaMays
  • 1,073
  • 7
  • 7
  • 2
    xsendfile will exit PHP and release the memory, and allow apache to send the file using DMA / hardware acceleration. If you use readfile you are passing the data through multiple buffers and duplicating it in memory (PHP reads file into memory -> php output buffer -> apache output buffer -> network buffer) all of which is done in the processor as opposed to sendfile (apache tells Harddrive to load file directly into memory using DMA, apache tells network card to send data by reading directly from memory, processor is free to do other things instead of needless memory copies) – msEmmaMays Jun 20 '13 at 22:52
  • I was not debating the merits of xsendfile, but the print in "the old fashion way". First even echo is faster than print and second readfile is faster than both of them, especially if you have to file_get_contents something prior to echoing it. – Eugene Kuzmenko Jun 29 '13 at 20:15
0

This is not correct. If XSendFile is not set to On for the relevant location but the module is loaded, it will falsely assume, that xsendfile usage is available, though its not.

Glenn
  • 1