Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if glob() is allowed

Tags:

php

glob

I want to use glob() in a php script, but I got reports from users that it's not always allowed on the server. One user reported the following error:

Warning: glob() has been disabled for security reasons

How do I detect whether glob is allowed? Is this done via disable_functions, or are there other ways glob can be disabled? Does safe_mode also disable glob? (one commenter on php.net says so).

Are there ways to reliably check if it's allowed other than checking safe_mode & disable_functions (as suggested in: how to test if PHP system() function is allowed? and not turned off for security reasons)

like image 256
Ewout Avatar asked Mar 17 '23 16:03

Ewout


2 Answers

The glob() function returns NULL if it's disabled, so:

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

Btw, this won't prevent a warning from showing up; you can choose to either silence or ignore it altogether.

like image 142
Ja͢ck Avatar answered Mar 26 '23 02:03

Ja͢ck


AFAIK glob can be only disabled using the disable_functions ini setting. Use function_exists() to detect if it is available:

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

You can try it using this 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)
like image 45
hek2mgl Avatar answered Mar 26 '23 03:03

hek2mgl