1

Is it possible to execute cmd commands in Windows OS with PHP exec() function?

I tried this:

<?php

try {

    echo exec(
    'O:\test\pdftk.exe O:\test\outputs\OPP\out.pdf O:\test\outputs\OPP\out2.pdf cat output O:\test\123.pdf'
    );

} catch (Exception $e) {
    echo $e->getMessage();
}

Basically, I'm trying to merge two pdf files with the pdftk program. If I just write the same exact command to the cmd by hand, it works and the O:\test\123.pdf file is created. But when I execute the above PHP file, nothing happens (blank page, the file is not created).

4 Answers 4

2

Can your PHP user access cmd.exe? You might find the tools at Microsoft's Sysinternals very useful; particularly the process monitor.

Sign up to request clarification or add additional context in comments.

3 Comments

I think yes. Other commands do work. If I try exec('ipconfig -all'), for example, it works. I downloaded the process monitor and it seems that every process with pdftk gets executed correctly. There is just one process which says "buffer overflown" or something like that.
Can you confirm that if you open cmd.exe and run 'O:\test\pdftk.exe O:\test\outputs\OPP\out.pdf O:\test\outputs\OPP\out2.pdf cat output O:\test\123.pdf' that it works as expected?
Yes. It works as expected when I type it directly into shell.
1

Try escaping the directory separator:

exec("O:\\test\\pdftk.exe O:\\test\\outputs\\OPP\\out.pdf O:\\test\\outputs\\OPP\\out2.pdf cat output O:\\test\\123.pdf");

Or even better, use single quotes instead:

exec('O:\test\pdftk.exe O:\test\outputs\OPP\out.pdf O:\test\outputs\OPP\out2.pdf cat output O:\test\123.pdf');

7 Comments

No. That doesn't work. I also tried shell_exec() which doesn't work as well.
@Richard: Even still, you will need to correctly escape the directory separator or use single quotes instead. Have you made sure exec and shell_exec are enabled in your configuration?
@Richard Knop try something simpler like writing some text to a file with shell_exec then maybe something else is wrong and not with php configuration(that is if exec is enabled)
exec() is enabled. I tried exec('ipconfig -all'); and shell_exec('ipconfig - all'); and both worked.
@Richard: That would suggest there's a problem with your command. Try passing an array to the second parameter of exec, then print_r that array afterwards.
|
0

Here is a project that allows PHP to obtain and interact dynamically with a real cmd terminal. Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

//if you prefer Powershell, replace 'cmd' with 'powershell'
$shellObj    = \MTS\Factories::getDevices()->getLocalHost()->getShell('cmd');

$strCmd1   = 'O:\\test\\pdftk.exe O:\\test\\outputs\\OPP\\out.pdf O:\\test\\outputs\\OPP\\out2.pdf cat output O:\\test\\123.pdf';
$return1   = $shellObj->exeCmd($strCmd1);

The return will give you the command return OR error from cmd, just as if you sat at the console. Furthermore, you can issue any command you like against the $shellObj, the environment is maintained throughout the life of the PHP script. So instead of bundling commands in a script file, just issue them one by one using the exeCmd() method, that way you can also handle the return and any exceptions.

Comments

-1

try Executing using the Admin Privileges for command prompt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.