Check if glob () is allowed

I want to use glob()a script in PHP, but I have reports from users that it does not always allow on the server. One user reported the following error:

Warning: glob() has been disabled for security reasons

How to determine if glob is allowed? Is this done with disable_functions , or are there other ways you can disable glob? Does safe_mode also disable glob? (one commenter on php.net says so ).

Are there any ways to reliably check if this is allowed by others other than checking safe_mode and disable_functions (as suggested in: how to check if the PHP system () function is enabled? And not disabled for security reasons )

+4
source share
2 answers

AFAIK globcan only be disabled using the disable_functionsini parameter . Use function_exists()to determine if it is available:

if(function_exists('glob')) {
    glob('...');
}

You can try this using these simple tests:

you@server ~ $ php -ddisable_functions='glob' -r 'var_dump(function_exists("glob"));'
bool(false)

you@server ~ $ php -r 'var_dump(function_exists("glob"));'
bool(true)
+3
source

The function glob()returns NULLif it is disabled, therefore:

if (($res = glob('*')) === null) {
    //try something else
} else {
    // $res should be an array or false
}

Btw, this does not prevent the warning from appearing; You can either disable or completely ignore it.

+4
source

All Articles