0

Possible Duplicate:
PHP background process in safe mode

My project is running on shared host and so safe mode is enabled. I wanted to use exec() function but it's not possible. what should I do in this case?
Is there a same function or solution which works on safe mode?

Community
  • 1
  • 1
Aliweb
  • 1,891
  • 3
  • 21
  • 44
  • move hosts. really is the best option. Many shared hosts allow exec() and safe mode never was –  Sep 20 '12 at 20:06
  • That's changing the problem , not a solution – Aliweb Sep 20 '12 at 20:12
  • 2
    Most good hosts don't have this restriction - so of course its a solution. There are millions of hosts, and not all are suitable for particular projects. If they impose this limit via 'code' any way around it would probably be a breach of their terms - so not a robust solution anyway. –  Sep 20 '12 at 20:15
  • Depends on the context in which you need to execute the script. You can't execute it from php with safe mode on, but you may be able to execute it some other way. – Explosion Pills Sep 20 '12 at 20:22
  • wait to the bad hosts upgrades their php version, safe mode was removed in 5.4.0, it was never safe. –  Sep 20 '12 at 20:30
  • I want to run another script. There some solutions like *curl* which isn't efficient and increases execution time. I want something like the function *exec()* to do that – Aliweb Sep 20 '12 at 20:30
  • Is it possible to insert the script to host cgi-bin directory and make a HTTP request? – Antti Rytsölä Sep 20 '12 at 20:31
  • 1
    The whole point of safe mode is to prevent using the exec(). – Antti Rytsölä Sep 20 '12 at 20:32

1 Answers1

0

This function combines all possibilities I am aware of. Try it. I hope, it helps.

<?php
function _exec($cmd) {
    $disablefunc = array();
    $disablefunc = explode(",", str_replace(" ", "", @ini_get("disable_functions")));
    if(is_callable("exec") && !in_array("exec", $disablefunc)) {
        exec($cmd, $result);
        $result = join("\n", $result)."\n";
    } elseif(is_callable("system") && !in_array("system", $disablefunc)) { 
        $src = @ob_get_contents();
        @ob_clean();
        system($cmd);
        $result = @ob_get_contents();
        @ob_clean();
        echo $src;
    } elseif(is_callable("passthru") && !in_array("passthru", $disablefunc)) {
        $src = @ob_get_contents();
        @ob_clean();
        passthru($cmd);
        $result = @ob_get_contents();
        @ob_clean();
        echo $src;
    } elseif(is_callable("popen") && !in_array("popen", $disablefunc) && is_resource($h = popen($cmd, "r"))) {
        $result = "";
        if(is_callable("fread") && !in_array("fread", $disablefunc)) {
            while(!feof($h)) {
                $result .= fread($h, 1024);
            }
        } else {
            while(!feof($h)) {
                $result .= fgets($h, 1024);
            }
        }
        pclose($h);
    } else {
        trigger_error("Cannot execute the command due to server restrictions.", E_USER_WARNING);
        return false;
    }
    return $result;
}

echo "<pre>"._exec("ls")."</pre>";
?>
flxapps
  • 1,066
  • 1
  • 11
  • 24