how to run cmd in php exec function - php

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);

Related

PHP exec command "ng --version"

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).

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.

Rscript behaving differently when run from commandline and from PHP exec

Hi I'm running an Rscript with PHP exec and it is behaving strangely.......It launches R but throws an error at the following line:-
filein = filein[,c("id","bank","trans_date","description","description_2",
"description_3","description_4","description_5" ,"type",
"debit","credit","statement_balance", "cleared_balance",
"debit_int_rate","credit_int_rate","category")]
This simple rearranges the columns in a data-set.
It throws the following error:-
Error in
[.data.frame`(filein, , c("id", "bank", "trans_date", "description",
: undefined columns selected
However I run the same script from command-line it runs without any error.
(I'm running the Rscript on a ubuntu 14.04 machine with PHP5......also when I run the same script on a windows machine from PHP it also runs perfectly)
Anybody have any ideas why this is?
Thanks for all of your help....So it wasn't a permissions issue(I had fixed those previously).......the issue was:----The command passed to commandline by PHP exec was
sudo /Rscript /home/xin/Documents/ClassificationApp/ClassificationAllInOne.R "http://localhost/categorisation/public/classification/data/1423746975.json"
However the argument that was received by R was "localhost/categorisation/public/1423746975.json2";
For some reason the Ubuntu/PHP combination added a 2 to the end of the argument string....I added a line in ClassificationAllInOne.R to strip the 2 and it worked perfectly

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

PHP shell_exec() - having problems running command

Im new to php shell commands so please bear with me. I am trying to run the shell_exec() command on my server. I am trying the below php code:
$output = shell_exec('tesseract picture.tif text_file -l eng');
echo "done";
I have the picture.tif in the same directory as the php file. In my shell I can run this without a problem.
It takes a while to run the code, then it doesnt make the text_file like it does when I run it in command prompt.
Per your comment:
Should I write a loop in shell
instead?
You can write a very simple shell script to run the command in a loop. Create the script file first:
touch myscript.sh
Make the script executable:
chmod 700 myscript.sh
Then open it with a text editor such as vim and add this:
for (( i = 0 ; i <= 5; i++ ))
do
tesseract picture.tif text_file -l eng
done
Thats the very basics of it (not sure what else you need), but that syntax should help get you started. To run the script, do this if you're in the same directory as the script:
./myscript.sh
Or specify the full path to run it from anywhere:
/path/to/mydir/myscript.sh
Could this be a permissions issue? My guess is that PHP isn't running with the same permissions that you do when you execute the command directly from the command prompt. What OS are you running on?

Categories