Reputation: 6335
I have this problem: On a ISS web server, windows 7 x64 professional, zend server installed. Running this command under php:
exec('dir',$output, $err);
$output is empty, $err=1.
So exec is not returing the output, it seems has some errors.
Php disable_functions
is empty, php is not on safe mode, is standard, I check all option.
It seems to be a general errors, even search on google do not give results.
Please write every one his experince and eventual solutions or workarounds.
Upvotes: 9
Views: 29353
Reputation: 101
If you are on Windows machine and trying to run an executable like this:
shell_exec("C:\Programs\SomeDir\DirinDir\..\DirWithYourFile\YourFile.exe");
and there is no output or your file isn't started, then you should try this:
chdir("C:\Programs\SomeDir\DirinDir\..\DirWithYourFile");
shell_exec("YourFile.exe");
It should work.
This is particularly handy if one is using relative path from folder where script currently is being located, for example:
$dir = dirname(__FILE__).'\\..\\..\\..\\web\\';
And don't forget to chdir() back to originating folder if you need to run other programs.
Upvotes: 1
Reputation: 8915
EDIT: After a few researchs, the only way i see to execute the DIR command is like that:
cmd.exe /c dir c:\
So you can try my solution using:
$command = 'cmd.exe /c dir c:\\';
You can use proc_open function too, which gives you access to stderr, return status code and stdout.
$stdout = $stderr = $status = null;
$descriptorspec = array(
1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
2 => array('pipe', 'w') // stderr is a pipe that the child will write to
);
$process = proc_open($command, $descriptorspec, $pipes);
if (is_resource($process)) {
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// 2 => readable handle connected to child stderr
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$status = proc_close($process);
}
return array($status, $stdout, $stderr);
The interesting part with proc_open, compared to other functions like popen or exec, is that it provides options specific to windows platform like:
suppress_errors (windows only): suppresses errors generated by this function when it's set to TRUE
bypass_shell (windows only): bypass cmd.exe shell when set to TRUE
Upvotes: 5
Reputation: 538
Try this:
shell_exec('dir 2>&1');
It works fine on Windows 8.1 64bits with UAC enabled.
Upvotes: 3
Reputation: 11
I was the same problem and after spend a lot of hours and cup of coffee
Just Disabling User Account Control (UAC) in Windows 7 short path :P C:\Windows\System32\UserAccountControlSettings.exe and select "Never notify"
and php exec() works fine!
I use:
Apache Version : 2.2.11
PHP Version : 5.2.8
Windows 7 SP1 32bit
Best regards! :D
Upvotes: 1
Reputation: 6335
I know I have choosen an answer as I must do it but I still do not understand why it wont work on my macchine but I found a fallback (using proc_open) solution using another method:
public static function exec_alt($cmd)
{
exec($cmd, $output);
if (!$output) {
/**
* FIXME: for some reason exec() returns empty output array @mine,'s machine.
* Somehow proc_open() approach (below) works, but doesn't work at
* test machines - same empty output with both pipes and temporary
* files (not we bypass shell wrapper). So use it as a fallback.
*/
$output = array();
$handle = proc_open($cmd, array(1 => array('pipe', 'w')), $pipes, null, null, array('bypass_shell' => true));
if (is_resource($handle)) {
$output = explode("\n", stream_get_contents($pipes[1]));
fclose($pipes[1]);
proc_close($handle);
}
}
return $output;
}
Hope this helps someone.
Upvotes: 1
Reputation: 539
You can check permisssions of IIS User to cmd.exe
cacls C:\WINDOWS\system32\cmd.exe
If in output is line like (COMPUTERNAME=your computer name):
C:\WINDOWS\system32\cmd.exe COMPUTERNAME\IUSR_COMPUTERNAME:N
It means that user has no rights to execute cmd.
You can add execute permissions with command:
cacls C:\WINDOWS\system32\cmd.exe /E /G COMPUTERNAME\IUSR_COMPUTERNAME:R
Upvotes: 0
Reputation: 457
The main reason you get zero output from a command such as dir
is because dir
doesn't exist. It's part of the command prompt, and the solution to this particular problem is well, in this page somewhere.
You can find this out by pressing WIN + R
, and typing in dir
or start
for that matter - an error message appears!
However perverse this may sound, I've found that the most reliable way to do anything process related in Windows is with using the Component Object Model. You said for us to share our experiences, right?
$me hears people laughing
Regained your composure yet?
So, first we'll create the COM object:
$pCOM = new COM("WScript.Shell");
Then, we'll just run what ever needs to be run.
$pShell = $pCom->Exec("C:\Random Folder\Whatever.exe");
Cool! Now, that'll hang until everything's finished in that binary. So, what we need to do now is grab the output.
$sStdOut = $pShell->StdOut->ReadAll; # Standard output
$sStdErr = $pShell->StdErr->ReadAll; # Error
There's some other things you can do - find out what was the process ID, error code, etc. Although this example is for Visual Basic, it's based on the same scheme.
Upvotes: 10
Reputation: 813
For security purposes, your server may have restricted access to "exec" command. In this case, maybe you should contact the hosting company to remove this restriction (if there is one).
Upvotes: 0
Reputation: 21553
There are a few posts to the relevant sections of the PHP Manual such as this one:
I was having trouble using the PHP exec command to execute any batch file. Executing other commands (i.e., "dir") works fine). But if I executed a batch file, I receieved no output from the exec command.
The server setup I have consists of Windows Server 2003 server running IIS6 and PHP 5.2.3. On this server, I have:
- Granted execute permissions to the Internet User on c:\windows\system32\cmd.exe.
- Granted Everyone->Full Control to the directory in which the batch file is written.
- Granted Everyone->Full Control on the entire c:\cygwin\bin directory and its contents.
- Granted the Internet User "log on as batch" permissions.
- Specified the full path to each file being executed.
- Tested these scripts running from the command line on the server and they work just fine.
- Ensured that %systemroot%\system32 is in the system path.
It turns out that even with all of the above in place on the server, I had to specify the full path to cmd.exe in the exec call.
When I used the call:
$output = exec("c:\\windows\\system32\\cmd.exe
/c $batchFileToRun");
then everything worked fine. In my situation,
$batchFileToRun
was the actual system path to the batch file (i.e., the result of a call to realpath()).
There are a few more on both the exec
and shell_exec
manual pages. Perhaps following through them will get it up and working for you.
Upvotes: 15
Reputation: 61098
Take a look at Apache error_log, maybe you'll find the error message.
Also, you can try using popen instead of exec. It gives more control because you can separate process start from output read:
$p = popen("dir", "r");
if (!$p)
exit("Cannot run command.\n");
while (!feof($p))
echo fgets($p);
pclose($p);
Upvotes: -1