A few days ago I published this thread, and it was solved succesfully on Unix.
Now I moved to Windows and I have the same problem.
I need to execute a command from the web application. It must execute a .jar stored in the system. I looked on Google and found some changes that I needed to do.
The function to execute the command is:
<?php
if($_POST["name"] == "")
echo "name is empty";
else{
$path = $_POST["name"];
//$command = 'DISPLAY=:0 java -jar '.$path'; -> Used in Unix
//$command = 'java -jar ../../../../simulaciones/tanqueCalentamiento.jar';
$command = 'java -jar ..\..\..\..\simulaciones\tanqueCalentamiento.jar';
//system($command);
exec($command);
}
?>
The comments are different options I have tried. $path is the argument, but decided to put directly the path, to clarify my question:
Do I need a kind of "trick" as I did for Unix?
The one I use:
function _exec($cmd)
{
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run($cmd, 0,false);
error_log($cmd);
return $oExec == 0 ? true : false;
}
/!\ If your application has an interface, and if you are using vista or 7, your server can't be run as a service!
You're just starting 'java'. That means that the Java executable (java.exe) must exist, and must be able to be executed. Since you don't specify a path, it must be in a directory that is in the %PATH% environment variable.
To test, simply start cmd and type java<enter>. If java gets executed, you'll know at least you'll have java installed and startable. If it doesn't, check if you have java and specify the correct path to the java executable.
You can also run the .jar from the command line to see if it works. If it works like this, it is likely to work in PHP too.
In windows vista, 7 (I don't know if in others too), there is a problemm with xampp.
The apache does not stop, doesn't matter what you do, even switch the computer off. At least for me, the solution was:
Uninstall xamp and reinistall it.
Start/stop it always as administrator:
path\xampp\xampp_start
path\xampp\xampp_stop
Right click: run as administrator.
In this way apache is finished properly, and I can run my command with no probleems anymore
I hope this helps
http://drupal.org/node/224342
Related
i'm pretty new to PHP and i need to compile a vb6 project from windows command prompt.
I've tried everythig but nothing seems to work, even creating a batch and execute from PHP code, no way... (the batch works if launched from cmd itself).
I also defined variables with various paths with no success.
i.e.:
$cmd = ('vb6.exe -MAKE D:\Websites\devopservices\dlledit\portaleeditorisen\NewSecurity\Data\NewSecurity_data.vbp D:\Websites\devopservices\dlledit\compiled\test.dll');
echo exec($cmd);
or inserting paths with variables:
$proj = 'D:\Websites\devopservices\dlledit\portaleeditorisen\NewSecurity\Data\NewSecurity_data.vbp';
$dll = 'D:\Websites\devopservices\dlledit\compiled\test.dll';
here it is the full command i need to execute:
<?php
echo exec('"C:\Program Files (x86)\Microsoft Visual Studio\VB98\VB6.EXE" /MAKE D:\Websites\devopservices\dlledit\portaleeditorisen\NewSecurity\Data\NewSecurity_Data.vbp D:\Websites\devopservices\dlledit\compiled\NewSecurity_Data.dll');
?>
Thanks for your help.
I resolved, it was not a php issue but related to identity in IIS appilcation pool :-)
I'm running PHP 5.4.9 on Windows server
I've tried running all script commands in PHP (exec, shell_exec, system, proc_open, passthru). All seem to return empty or null.
I've added phantomjs as a PATH variable.
And running phantomjs --version in command prompt, and it returns 1.8.2
Although when I try to run
$return = exec("phantomjs --version")
or
$return = shell_exec("phantomjs --version", $output)
$return is always null and $output is empty.
I made sure IUSR and IIS_IUSRS users have permission to run phantomjs.exe
Safe mode is disabled in php.ini
Also, I tried running exec('ls') && exec('ipconfig /all'), and those output the data I'm expecting.
I'm not sure what else to try.
I was facing the same problem..
The thing is phantomjs requires complete path for all
Here is the solution I came up with:
$getout = exec('D:\\xampp\\htdocs\\phantomjsdir\\phantomjs.exe D:\\xampp\\htdocs\\rasterisejsdir\\rasterize.js http://localhost/pagetobecaptured/test D:\\xampp\\htdocs\\outputfiledir\\test2.jpg "1800px*840px"',$o,$e);
You are pretty close to a solution. It's basically:
$stdout = shell_exec('time /T');
echo $stdout;
You need to make sure, that the Phantom binary is either on path or called with full-path.
For a full example executing PhantomJS, see the driver file of "jakoch/PHPUnit-headless".
I know this topis is there already, but none of the answers helped me... I copied wkhtml folder from my HDD onto the server. When I run
exec('"../wkhtmltopdf/wkhtmltopdf.exe" "www.estiroad.com/export.php" "C:/EXTREM.pdf"');
nothing happens... Do I type the paths correctly? I mean, I need to type exact path to the wkhtmltopdf according to where I run the exec command from, right? And if I want to save it onto users HDD, I need to use absolute path, right? Strange is, that it gives me no error, just silently does nothing... I found about PHP bindings, but I donĀ“t understand how to use them... Everybody solves that problem only in Linux and thats worthless for me :( Any help will be appreciated.
You should't be putting the quotes around the library.You can catch the output of the command this way:
$commandString = '../wkhtmltopdf/wkhtmltopdf-i386 http://www.estiroad.com/export.php file.pdf 2>&1';
$output = shell_exec($commandString);
The 2>&1 in UNIX will mean that the output will come through. 1 is stdout. 2 is stderr.
Hope this helps.
Or in windows
$commandString = '../wkhtmltopdf/wkhtmltopdf.exe http://www.estiroad.com/export.php file.pdf 2> output';
print $out ? $out : join("", file("output"));
From the permission issue it looks like you're running the production script on linux. Go to your production server and run
$ uname -a
You'll get something like:
Linux ora100 2.6.5-7.252-smp #1 SMP Tue Feb 14 11:11:04 UTC 2006 x86_64 x86_64 x86_64 GNU/Linux
the x86_64 suggest you're running a 64 bit CPU, if that's the case download the amd64 version of binary, otherwise download the i386 one. Both can be obrained from this url: http://code.google.com/p/wkhtmltopdf/downloads/list
Keep the windows binary. Have you got a config file? if you do make sure you have a switch where you assign your library path to a constant based on your environment.
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// this is windows server
define('WKHTML_LIB', "../wkhtmltopdf/wkhtmltopdf.exe");
} else {
// or the 64 bit binary?
define('WKHTML_LIB', "../wkhtmltopdf/wkhtmltopdf-i386");
}
Then change your code that initiates wkhtmltopdf:
$commandString = WKHTML_LIB' http://www.estiroad.com/export.php file.pdf 2> output';
print $out ? $out : join("", file("output"));
Use snappy from knplabs. Its a wrapper around wkhtmltopdf and handles the cases.
(I have experience with wkhtmltopdf, but ONLY on *nix)
My advice: First do EVERYTHING without PHP, no exec().
Simply make sure you can type on the commandline the command to create the PDF.
Are you sure you installed WebKit on the machine? wkhtmltopdf.exe depends on it.
Only after you are sure you can generate from commandline, try to translate your action to PHP. ALso make sure PHP has appropriate rights to execute wkhtmltopdf.exe, AND has appropriate right to write away to C:/EXTREM.pdf.
I have a problem trying to run passthru function in my php code (Joomla module). the code is following (this is only a snippet)
ob_start();
passthru("/usr/bin/whois 85.70.231.130 | /usr/bin/grep 'address:'",$code);
$whoisData = ob_get_contents();
ob_end_clean();
$whoisData = str_replace("address:", "", $whoisData);
$whoisArray = split("\n",$whoisData);
echo trim($whoisArray[1]);
when I run this on my localhost, it echoes what it should, but when I run this code on the production server, it echoes nothing and the $code variable contains 127 (command not found). I tryied add absolute paths to these commands into the passthru function, but it didn't helped. Interesting is, that when I run the code right from terminal via ssh and php command, it runs well, but when it's called from application context it doesn't. Does anybody know what I should to do?thanks
SOME EDITS..
safe_mode is on
webserver does not see into /usr/bin and /bin/ folders so what is the best way how to run these commands from php?
usr/bin/grep doesn't look like a valid path to a command.
The missing / at the beginning of the path to the second command might explain the command not found error... even if the first whois command is found.
Have you looked to see if your webserver / php is running chrooted?
print_r(glob('/*'));
if (file_exists('/usr/bin/grep') && file_exists('/usr/bin/whois')) {
print "maybe its a permissions thing?\n";
} else {
print "can't see executables required\n";
}
should give you a clue.
So I have already solved my problem with phpwhois library. I seems like with my server configuration is it unlikely that these functions will be working well. So thanks for your help:)
I have one server (test) which has "php" command that I can run CLI with. The other server (hosting) runs only with "php5-cli" command.
Example:
exec("php file.php"); // Works on test server
exec("php5-cli file.php"); // Works on hosting server
How can my script detect which one to use?
Make a config value specific to each server. On some servers you will encounter php5 as a cli command, instead of php:) And the system administrator may easily rename or symlink the executable to any name he likes.
edit:
try something along the lines of :
$output = Array();
$result = -1;
exec("php -f myScript.php", $output, $result);
if($result==-1){
// you should try to exec use the other method here
echo "app 'php' not found";
}else{
// the exec was a success
}
php-config --php-binary
php-config5 --php-binary
But you probably run into similar problems of no guarantee what that command is called (or that it exists). Not sure how many hosting companies use it.
Make sure which php.ini is included when using each of them.
Could you symlink php-cli as php or vise versa?
That way either would work.
Why not make a symlink to the executable on one of the servers ln -s so that one of them is available on both servers.
That should solve this problem.