PHP exec command "ng --version" - php

I want to check if the user has install angular cli with running ng --version on PHP with exec, proc_open or shell_exec but return is empty.
I dont have problems if I execute in cmd or in terminal's Visual Studio Code in any path on Windows.
I'm use wampserver

There are multiple ways to do it, and you have to consider what you will do if there are more than one found. You can find it:
exec('where /r C:\ ng.exe', $paths);
And then if you really need the version, use the first one if it starts with C:\:
if(strpos($paths[0], 'C:\\') === 0) {
exec($paths[0] . ' --version', $version);
}
print_r($version);
If it isn't found then $paths[0] should contain:
INFO: Could not find files for the given pattern(s).

Related

Run NodeJS commands from PHP

I have the following PHP code that is run when a user clicks a button on a local intranet page to run some Node JS commands. e.g.
exec('npm install', $output);
$output = implode(PHP_EOL, $output);
echo $output;
But the command doesn't seem to get run... It does if I type the command into the command prompt and run it...
I don't see any errors and NodeJS is set as a system variable in the Path so it should know what grunt is... Any ideas? Other commands, e.g. whoami run fine.
Any ideas?
I'm on Windows 7.
Update: Based on the comments below I can now get an error with:
exec('npm install 2>&1', $output, $returnCode);
$output = implode(PHP_EOL, $output);
echo $output . ' ' . $returnCode;
And this is the error:
TypeError: Cannot call method 'get' of undefined at C:\Program Files (x86)\nodejs\node_modules\npm\lib\npm.js:310:23 at C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npmconf\npmconf.js:80:7 at Array.forEach (native) at C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npmconf\npmconf.js:79:13 at f (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\once\once.js:16:25) at C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npmconf\npmconf.js:108:14 at Conf. (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npmconf\npmconf.js:179:14) at Conf.next (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npmconf\lib\load-prefix.js:48:20) at C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\mkdirp\index.js:37:53 at Object.oncomplete (fs.js:107:15) C:\Program Files (x86)\nodejs\node_modules\npm\lib\npm.js:33 throw new Error('npm.load() required') ^ Error: npm.load() required at Object.npm.config.get (C:\Program Files (x86)\nodejs\node_modules\npm\lib\npm.js:33:11) at exit (C:\Program Files (x86)\nodejs\node_modules\npm\lib\utils\error-handler.js:49:27) at process.errorHandler (C:\Program Files (x86)\nodejs\node_modules\npm\lib\utils\error-handler.js:316:3) at process.emit (events.js:95:17) at process._fatalException (node.js:272:26) 7
So it looks like the PHP knows where NodeJS and NPM is... but can't run it?
If I do whoami in the PHP I get: nt authority\iusr
But if I do it from the command prompt then I get: dom\cameron
I wonder if it's because PHP runs it as a different user to when I run it directly in the command prompt...
Okay I've managed to fix this!
Basically you need to run the website (where the PHP file lives) as the same user as where NodeJS is run from!
To achieve this I did the following in IIS:
1.) Find the website
2.) Choose basic settings
3.) Click the connect as... button
4.) Choose 'specific user'
5.) Enter `dom/cameron` and my password
6.) Restart IIS
If anyone has any ideas for how to get NodeJS running for the default nt authority\iusr then feel free to post an answer :)
You defined npm in WINDOWS path, but did PHP recognize it?
I suggest you should test it with writing the full NPM path in the exec. If it works, than you should call putenv() with correct parameters before executing npm install..
So first try:
exec('C:\npm-test\npm install', $output);
$output = implode(PHP_EOL, $output);
echo $output;
And if it works, use the putenv() command before calling exec(). Example:
putenv("npm='C:\npm-test\npm'");
//and then exec(...)
The error you're getting indicating that an object is not defined. It's quite possible that a module is simply not loading because you're not running the script from the correct location, and relative links to some include are not working. Running as the correct user might fix it. Or try changing to the correct directory before running the node script. Or change the location of the links inside the node script.

Not able to run wkhtmltopdf commad through shell_exec() function in php but same command works on command line

I'm in trouble and that much confused about a php shell_exec command.
When the command is execute by PHP I have no error but the execution fails. If I use exactly the same command from a terminal it works.
Here's the command :
/usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyxa9otq.html" "/tmp/knplabs_snappyv3pD7h.pdf"
When I lauch this from a terminal :
$ /usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyWG9XTd.html" "/tmp/knplabs_snappyv3pD7h.pdf"
Loading page (1/2)
Printing pages (2/2)
Done
But from my php script :
// Construct the previous command
$command = $this->buildCommand($url, $path);
../..
shell_exec($command);
../..
$content = file_get_contents($path);
../..
I've test the output of shell_exec, it's empty.
The log :
Warning: file_get_contents(/tmp/knplabs_snappyv3pD7h.pdf): failed to open stream: No such file or directory in /*****/lib/snappy/SnappyMedia.class.php on line 64
No permission pb in the /tmp directory :
$ ls -la /tmp
total 448
drwxrwxrwt 16 root root 4096 mars 12 21:51 .
../..
I've tried avec the PHP exec() function to get error informations, I just get an "1" error code in return_var and nothing in output.
For information this issue appear on my test server, my desktop computer but not on my notebook. All the 3 are with sames PHP, Apache, Mysql versions.
I don't understand anything ...
Thanks for any help, I'm loosing my mind.
David.
I've found the solution here : Executing wkhtmltopdf from PHP fails
Thanks to Krzychu.
First to get information from the shell_exec command add " 2>&1" at the end of the command. In that way you will get information in return of the command :
$no_output = shell_exec($command);
echo $no_output; // nothing
$output = shell_exec($command . ' 2>&1');
echo $output; // in my case : "cannot connect to X server"
The solution :
Not use the wkhtmltopdf ubuntu package (0.9.9-4)
Use the official package from the Wkhtmltopdf download page
So no need to install xvfb ! (I've seen this advice many times)
Looks like a user's permissions issue.
When you run the command from the terminal, it is the user account, currently used, which does have the right permissions, to run a command in /usr/bin, and execute the specific file.
When you run it from the php script, it is the http server account on your system, which needs the permission to execute the file in /usr/bin. Usually this is the apache user.
How you should setup permissions depends on your system. Just remember that what is allowed for apache, is allowed for anyone accessing your http server.
I have had this problem for ages and adding . ' 2>&1' after the $command has somehow solved the problem.
this:
$output = shell_exec($command . ' 2>&1');
instead of:
$output = shell_exec($command);
No idea why but it works and I'm grateful.
Is it a shared hosting? It seems like shell_exec is a restricted function. Try running error_reporting(E_ALL); ini_set('display_errors', 1); before calling shell_exec.
I stumbled upon the same Problem, in my case an absolut Path in the exec Command like /var/www did not work, I had to use relative Paths from the point where I executed the php File.
I also wanted to notice, that it did not work using shell_exec, however it worked using normal exec command, not sure wheres the difference here.

how to run cmd in php exec function

I am trying to run a command on windows which is the follwing :
sfm+pmvs C:\\xampp\\htdocs\\temp\\$filename/ hello.nvm
I have done this but it wont work for me :
exec("C:\\xampp\\htdocs\\temp\\draw\\VisualSFM_win32.exe sfm+pmvs C:\\xampp\\htdocs\\temp\\$filename/ hello.nvm");
when I do the following :
exec("C:\\xampp\\htdocs\\temp\\draw\\VisualSFM_win32.exe");
it works just fine but I need the other paramters to be included in the command line
how could that be done in php and is there any way to open a cmd.exe and to run the command above ?
thanks in advance
Exec() just running in cmd command which you wrote as first argument of exec(). If VisualSFM_win32.exe not support arguments by run via cmd, you cannot run it as you want. Sorry for my english =)
You asked me for exapmle. My example below.
I am not sure it is 100% right because i don't use windows last 3 years.
#echo off
cd "C:\xampp\htdocs\temp\draw\"
start VisualSFM_win32.exe sfm+pmvs C:\xampp\htdocs\temp\%1/ hello.nvm
and in php
exec("C:\\test.bat " . $filename);

Run pdftk by php on linux centos

I would like to run pdftk on my webserver. It's a Linux Centos with PHP 5.3.2.
When I connect it by commande line I do
pdftk --version
It's OK
pdftk A=p1-9.pdf cat A1 output p1.pdf
It's OK.
Now, I do this by php :
exec(pdftk A=p1-9.pdf cat A1 output p1.pdf)
It isn't OK. Why?? I search about the link of file, but it looks OK.
This doesn't work too :
exec(pdftk --version)
I install pdftk with this How do I install Pdftk on my server?
So what's wrong??
Thank for your help!
I've run into this issue before. Assuming that you're wrapping your commands string in quotes (as gioele noted), the issue may be that you need to set your path when running the system command. Try this:
$command = "pdftk A=p1-9.pdf cat A1 output p1.pdf";
system("PATH=\$PATH:/usr/bin/ && $command",$response);
if ($response===FALSE){
//there was an error, handle it
}
(I've added a little response handling there as well). If that doesn't work, check to see what path you should use (it will depend on where you installed PDFTK).
I believe you can also get the same result by using putenv("PATH=" .[your path]); and I've used system() here, but exec() should be affected in the same way

Is it possible to run php exec through cygwin?

Hey guys I have one php exec command that works on my remote linux server but not my windows (WAMP package) local server. I really want to create an identical workstation so I can test locally and then update my remote server. If anyone has any suggestions or solutions other than converting to linux, I would really appreciate it.
<?php
$safe_path = escapeshellarg('fake-virus.txt');
$command = '/usr/bin/clamscan --stdout ' . $safe_path;
$out = '';
$int = -1;
exec($command, $out, $int);
echo $int;
if ($int == 0) {
// all good;
} else {
echo 'o no';// VIRUS!;
}
?>
I really want to create an identical workstation so I can test locally and then update my remote server
Installing cygwin is the wrong way to go about this. Just use virtualbox, which you can get from here, and run an Ubuntu VM. Much easier (and nicer).
Use 'su' it works great for me when doing cool cross OS stuff
shell_exec('su Administrator -c "cmd/script"');
Its not that difficult to run cygwin with Apache PHP and more. Here is a pretty simple tutorial cygwin-apache-php Have fun!
At least in one instance I was able to use shell_exec() of php of wamp (on windows:-)) via cygwin. I had to install procps or something in the cygwin installer to have a top command of sorts in cygwin. Then I was able to use wamps php shell_exec to perform that cygwin top command using Kishans suggestion in http://php.net/manual/en/function.shell-exec.php:
<?php
echo $result = shell_exec("C:\cygwin64\bin\bash.exe --login -c '/cygdrive/c/cygwin64/bin/top.exe --b -n 1'");
?>
/////////////////
Kisha's remarks cited:
If you are on windows and has Cygwin, You can execute all your Cygwin binaries using shell_exec() as follows:
<?
$result = shell_exec("C:\cygwin\bin\bash.exe --login -c '/cygdrive/c/cygwin/bin/convert.exe --version'");
print($result);
?>
The key is the "--login" option which appends all the required common cygwin binary and library paths to $PATH. Without this option some of the cygwin binaries that links to other libraries ( Like ImageMagick binaries which links to X11 ) will not work and you may see errors like "cygX11-6.dll not found, Could not start convert.exe"

Categories