Question

When I login to Windows Server 2008 and run this code using cmd.exe (as an Admin), it works:

php -f e:\path\to\file\file-name.php go 4001

However, when I try to launch this from a php file, it does not:

$cmd = escapeshellarg("php -f e:\path\to\file\file-name.php go 4001");
exec($cmd);
exit; 

I've been at it for 6 hours and can't find a solution. I've even tried pstools to enable access to psexec, still no luck. I can't even get psexec to work from the command line because it seems that I'm unable to pass variables through (go and 4001).

Update

When I run the code using the PHP file, nothing happens, the file I want to be created does not get created and I'm not sure how to throw errors through this method.

<?php
if(function_exists('exec')) {
    echo "exec is enabled";
}
?>

Outputs, exec is enabled.

Please help!

Was it helpful?

Solution

I'm receiving no errors or warnings of any kind.

Well, you have this:

exec($cmd);

And the function signature is this:

string exec ( string $command [, array &$output [, int &$return_var ]] )

Change your code to make use of the optional arguments that provide error info.

In any case, you're encoding your complete command as a single argument. That cannot work: I count 5 different tokens in your command line, not one. You must encode all 5 items separately and you need to use different functions for commands and arguments.

Additionally, escapeshellarg() is basically a Unix-only function that has been poorly patched so it doesn't have obvious security issues on Windows, but it doesn't work properly on Windows, it'll often strip characters rather than escaping. I found a nicer replacement at the Symfony framework which is rather easy to adapt for standalone use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top