PHP - How to find out if X-Sendfile is available and installed?
Asked Answered
W

4

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 :)

Westney answered 2/9, 2012 at 4:25 Comment(1)
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.Lavernalaverne
R
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.

Rattat answered 2/9, 2012 at 4:35 Comment(2)
Save your "thanks" for someone else. I'm more than glad to have helped you out.Rattat
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.Carborundum
N
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.

Needy answered 2/9, 2012 at 4:35 Comment(0)
R
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);
    }
Roberge answered 2/9, 2012 at 7:51 Comment(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)Roberge
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.Lantana
S
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.

Scathe answered 13/5, 2013 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.