I am trying to run a .exe application with input file and argument.
With cmd I can successfully start the executable like this...
C:\Program Files\MyApp.exe "path\to\input file" argument
However, nothing happens when I simply copy paste the string above into the exec() function like this..
exec("C:\Program Files\MyApp.exe "path\to\input file" argument")
Do I need to escape parts of the string? How should I proceed?
Just pass the arguments like a normal calling from shell
ex:
exec("C:\Program Files\MyApp.exe \"path to\input file\" argument")
I had to use this format
php -q "./yii.php" migrate/up --interactive=0 --migrationPath=#vendor/pheme/yii2-settings/migrations
Related
I hope you can help me.
I am dealing with a problem that I cannot solve. This is my issue. I am trying to exec a bash script through PHP. I tried with the method
exec()
with 3 arguments, arg1, arg2, and arg3.
php code
<?php exec("./randomScript.sh arg1 arg2 arg3"); ?>
randomScript.sh
.... # random code which exploits the three arguments -> executed normally..
.... # random code which exploits the three arguments -> executed normally..
./secondScript.sh $arg1 $arg2 $arg3 #<- this is the script that is not running (is not even started).
I have tried to change the permission (I've got full permission), change the way I call the randomScript.sh (through absolute path), but nothing occurred. Besides, I tried with:
shell_exec()
method, but nothing changed. Of course, if I run the secondScript.sh via terminal everything works fine.
I cannot understand which is the problem. Could you please help me?
Thanks in advance.
In the inner shell, the arguments are not called like that (because it is invoked by the shell, not by PHP).
./secondScript.sh "$1" "$2" "$3"
or just
./secondScript.sh $*
At this stage, just remember that spaces, quotes, and dollar signs in the arguments you pass must be avoided at all costs (the solution is escaping them, but how to do it exactly is tricky).
You also might want to do instead:
$ret = shell_exec("./randomScript.sh 'arg1' 'arg2' 'arg3' 2>&1");
so that in $ret you might find error messages and output from the inner shell.
You should escape your string command before passing it in exec function, i think it may help you
Escapeshellcmd()
...
$escaped_command = escapeshellcmd("./randomScript.sh arg1 arg2 arg3");
exec($escaped_command);
...
Escapeshellarg()
For system command, i recommand using system
...
$escaped_command = escapeshellarg("./randomScript.sh arg1 arg2 arg3");
system($escaped_command);
...
You also need to make sure that your PHP code does not change working dir, and both shell scripts have execute permissions, and filesystems allows to exec files on it.
I would avoid exec("./script $arg ...");, I would rather specify the interpreter to use and full path to the script like exec("sh /home/user/project/script.sh $arg ...");
I want to convert a .shp file to .geojson format using ogr2ogr, which I can do from the (linux) command line.
However, rather than a command line conversion, I want to invoke the conversion from a php script (using e.g. exec(..) ) and send the output directly to a variable (as a character string) instead of writing to a file.
Is this possible?
You can use shell_exec() method available in PHP.
public function shptogeojson($shpfilepath,$output,$srsno,$srsold){
$query="ogr2ogr -f GeoJSON -t_srs EPSG:$srsno -s_srs EPSG:$srsold $output.shp $shpfilepath";
shell_exec($query);
}
How do I properly execute commands in the command line using php? For example I'm using the command below in the command line to convert a docx file into a pdf file:
pdfcreator.exe /PF"D:\Documents\sample.docx
Now using PHP code I want to be able to execute the same command but nothing seems to be happening:
<?php
shell_exec('pdfcreator.exe /PF"D:\Documents\sample.docx"');
?>
Is this possible in PHP?If yes, how do I do it?
system("c:\\path\\to\\pdfcreator.exe /PF\"D:\\Documents\\sample.docx"");
try this.
Don't forget to escape your command with escapeshellcmd(). This will prevent you from having to use ugly backslashes and escape characters.
There are also other alternatives which may work:
`command` // back ticks drop you out of PHP mode into shell
exec('command', $output); // exec will allow you to capture the return of a command as reference
shell_exec('command'); // will return the output to a variable
system(); //as seen above.
Also, make sure your .exe is included within your $PATH variable. If not, include the full path for the command.
I'd like to run something like (in myProgram.sh):
java -cp whatever.jar com.my.program $1
within PHP and read the output.
So far I have something like:
$processOrderCommand = 'bash -c "exec nohup setsid /myProgram.sh ' . $arg1 . ' > /dev/null 2>&1 &"';
exec($processOrderCommand);
But what I'd really like is to be able to get the output of the java program within the PHP script and not just execute it as another thread.
How can this be done?
You can do this :
exec($processOrderCommand, $output);
From the documentation :
If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().
For a better control on your execution you can take a look at proc_open()
Resources :
php.net - exec()
php.net - proc_open()
The key is that the classpaths need to be absolute within the shell_exec
PHP script.
Or at least that's the only way I could get it to correctly work. Basically it's almost impossible to tell from environment to environment what the relative directory is that the php script is running the JVM.
As well, it helped to put the absolute path location for java, such as usr/.../bin/java
i am trying to make a pdf file with wkhtmltopdf when i pass url www.example.com pdf is generating or www.example.com?id=1
but when i try to put another parameter command execution is not working
www.example.com?id=1&type=u
shell_exec("c:\pdf\wkhtmltopdf.exe
http://localhost/test/index.php?id=1&typee=abc
test.pdf ");
i try to use it via command line to but its not working there also
thanks for help
The & is causing your command to fail as it has special meaning in shell. Use escapeshellarg() to escape those characters first.
Use escapeshellarg() to escape parameters before passing them to the command line.
This is also mandatory when passing external data (e.g. user input) as parameters.