This question already has an answer here:
exec(): quoting full command in Windows
(1 answer)
Closed 9 years ago.
I am trying to use the exec command from PHP in an XAMPP environment on Windows.
When I use a simple command line and type
"c:\Program Files (x86)\whatever directory\sometool.exe" "c:\temp\test.dat"
everything works perfectly.
Then I use exec command of PHP like so:
<?php
exec('"c:\Program Files (x86)\whatever directory\sometool.exe" c:\temp\test.dat');
?>
it also works.
But when I include the quotes the tool seems to not get the parameter. That means
<?php
exec('"c:\Program Files (x86)\whatever directory\sometool.exe" "c:\temp\test.dat"');
?>
does not work. I have tried escaping and everything. I cannot find a solution. But I need quotes here in order to enable paths with spaces in the name. Can you point me into the right direction?
Save your path as a variable.. then exec that. Wrap the path in single quotes like below.
$cmd = '"C:\my path with spaces\targetapp.exe" C:\mypathnospaces\targetfile.xxx';
OR - try this fix for spaces in everything, trying to use escape character on the backslash, this is UNTESTED, let me know if it works or not.
$cmd = '"C:\my path with spaces\targetapp.exe" C:\\my other path with spaces\\targetfile.xxx';
exec($cmd)
Another solution would be to add the actual PATH to your windows environment, so that you do not need to call the path at all.
Related
This question already has answers here:
Php exec command not working on Windows, works on command line
(3 answers)
Closed 6 years ago.
When I execute the code below in command line it runs fine:
C:\Users\Shraddha\book ticket\ex1 scrapy crawl bookmyshow
However it doesn't execute in PHP using exec():
exec("C:\Users\Shraddha\book ticket\ex1 scrapy crawl bookmyshow");
You need to escape the blank characters in the path, otherwise they are interpreted as separator between multiple arguments. Also it is much safer to use the forward slash as folder separator as it is used in unixoid systems and the internet in general:
exec("C:/Users/Shraddha/book\ ticket/ex1\ scrapy\ crawl\ bookmyshow");
If you insist on MS-Windows style separators, then you have to escape them too:
exec("C:\\Users\\Shraddha\\book\ ticket\\ex1\ scrapy\ crawl\ bookmyshow");
Also you may prefer to use shell_exec() here to have a well defined and initialized environment for your command to be executed in.
It sounds very simple but I must be missing something here. I have a custom exe program, which is inside C:\dummy\dummytest.exe and I have a text file inside C:\text\test.txt . All I want to do is start dummytest.exe by passing test.txt as argument in PHP. Here is what I tried:
$arg = "C:\text\test.txt"
exec("C:\dummy\dummytest.exe".$arg);
I tried with just single '\' also. And I tried
exec("C:\dummy\dummytest.exe $arg"); but nothing seems to work. I get C:\dummy is not recognized as an internal or external command, operable or batch file error.
When I go to command line manually and do
C:\dummy\dummytest.exe test.txt
the application runs just fine. What am I missing here with exec?
Use the "shell_exec" command instead.
shell_exec("[BAT or EXE-File] [Params]");
Hope this helps!
EDIT
When using executables and parameters with paths you have to quote them.
So, an example would look like this:
echo nl2br(shell_exec("\"F:\\N3V Games\\Trainz Simulator 12\\compile_gs.bat\" \"F:\\xampp\\htdocs\\SHELL\\EBuLa.gs\""));
This example prints the output of a CMD-Window directly to the page.
If the executable is placed in the same directory like the php-file you can just run:
echo nl2br(shell_exec("compile_gs.bat EBuLa.gs"));
I am trying to use PHP to compile and upload an Arduino sketch through the command line. Right now a user uploads an ino or pde file through a form and it is transferred to a directory for later use. Using the uploaded file's location as a variable, I would like PHP to run the command line version of Ardunio to compile and upload it.
After an inital try with using exec() and system(), I switched to popen(). Running the following code I can get Arduino to open then it closes without uploading the sketch:
pclose(popen('"C:\Program Files\Arduino\arduino.exe" --port COM3 --upload "C:\sketches\uploads\cube\a\a.ino"));
Running that code and its variations through the Windows Command Line works so I know the input string is not the issue. Also, looking at the Windows Task Manager shows it opening for a second or so then closing. Could someone point me in the right direction?
For popen (or any of the other process functions) to work correctly on Windows you need to escape backslashes like this:
pclose(popen('"C:\\Program Files\\Arduino\\arduino.exe" --port COM3 --upload "C:\\sketches\\uploads\\cube\\a\\a.ino"'));
alternatively try replacing the backslashes with forward slashes. The following should also work on recent versions of Windows:
pclose(popen('"C:/Program Files/Arduino/arduino.exe" --port COM3 --upload "C:/sketches/uploads/cube/a/a.ino"'));
(Your code snippet was also missing a trailing single quote, but I suspect that's a typo.)
I'm trying to execute a php script from a php file, but it seems that i can't get the correct bin path of the php.exe. Its not doing anything as far as i can tell. Lets assume service-oauth.php is a simple echo.
EDIT3:
I fixed it using php-cli instead of php or the php bin path, i must admit that i tried this before but it seems something else was off when i tried this (one of the first things i tried). The answer provided by Keith in Can't execute PHP script using PHP exec , so it ended being a duplicate :S, thanks for the help to those who commented.
EDIT2:
I tried calling the script directly from the server console as #Dagon suggested and it works, both using the php env variable and the php path to the bin, its clear that the path is correct, but something is preveting to get the output or to run the script using the php exec() function
service-oauth.php
<?php echo "Hello there"; ?>
And this is my script:
$basePath = plugin_dir_path( __FILE__ ); # Wordpress function, asume it works.
$fileToExc = $basePath . 'service-oauth.php';
# PHP_BINDIR: /usr/local/bin
# PHP_BINARY: /usr/bin/php
# exec("which php") /usr/bin/php
$phpPath = exec("which php");
$output = exec("$phpPath $fileToExc");
print_r($output);
There are a lot of answers on stackoverflow that recommend any of those 3 options i commenented in the code, but none of them seems to work, not sure if it is the path or something else that is not working. I've tested this script on my localmachine (windows) and it works (even though i had to use a hardcoded path to the bin since i wans't getting the correct path), but i'm testing on my production server (linux) and is not working.
Note: Let me be clear, that this is not a duplicate, i've tried the following answers in these posts and many others, and it didn't work for me:
How to get path of the php binary on server where it is located
Can't execute PHP script using PHP exec
PHP exec to run a file
How to call shell script from another shell script?
Execute a PHP script from another PHP script
I've also tried using .exe at the end of the binary(windows localmachine), using php-cli instead of php, and i've tested the excec function and it works, but not in this case. Also tried with a shebang in the called script.
It's propably something simple that i'm not aware of but i've been spending a lot of hours searching and testing and nothing so far. Any help is appreciated.
EDIT:
Using scandir on the bin folder shows
scandir("/usr/bin/")
Array(
...
[632] => php
[633] => php-cgi
[634] => php-cli
...
)
Tested if in safe mode using ini_get('safe_mode'), but it seems off.
I fixed it using php-cli instead of php or the php bin path, i must admit that i tried this before but it seems something else was off when i tried this (one of the first things i tried). The answer provided by Keith in Can't execute PHP script using PHP exec , so it ended being a duplicate :S, thanks for the help to those who commented. – Zagen
I am using PHP on Windows machin. I also use Dev C++. I can perfectly compile .cpp file on CMD using this command:
g++ hello.cpp -O3 -o hello.exe
Now what I am trying to do is running the same command using php system() function, so it looks like this:
system("g++ c:\wamp\www\grader\hello.cpp -O3 -o C:\wamp\www\grader\hello.exe");
but it doesn't compile. I am lost, please tell me what am I missing?
I also looked up at this question and thats exactly what I need, but I couldnt find a usefull solution for my case there:
Php script to compile c++ file and run the executable file with input file
Use the PHP exec command.
echo exec('g++ hello.cpp -O3 -o hello.exe');
should work.
There's a whole family of different exec & system commands in PHP, see here:
http://www.php.net/manual/en/ref.exec.php
If you want the output into a variable, then use :
$variable = exec('g++ hello.cpp -O3 -o hello.exe');
If that doesn't work, then make sure that g++ is available in your path, and that your logged in with sufficient enough privliges to allow it to execute.
You may find also that it's failing beacuse PHP is essentially being executed by your web server (Unless your also running PHP from the cmd prompt) , and the web server user ID may not have write access to the folder where G++ is trying to create the output file.
Temporarily granting write access to 'Everybody' on the output folder will verify if that is the case.
Two things:
You are using double quotes and are not escaping the \ inside the path.
You are not using a full path to g++.
The first one is important as \ followed by something has a special meaning in such a string (you might know \n as new line), the second one is relevant since the PHP environment might have a different search path.
A solution might be
system("c:\\path\\to\\g++ c:\\wamp\\www\\grader\\hello.cpp -O3 -o C:\\wamp\\www\\grader\\hello.exe");
Alternatively you can use single quotes, intead of double quotes, they use diffeent,less strict escaping rules
system('c:\path\to\g++ c:\wamp\www\grader\hello.cpp -O3 -o C:\wamp\www\grader\hello.exe');
or use / instead of \, which is supported by windows, too.
system("c:/path/to/g++ c:/wamp/www/grader/hello.cpp -O3 -o C:/wamp/www/grader/hello.exe");
What you do is your choice, while many might consider the first one as ugly, and the last one as bad style on Windows ;-)
Thanks to everyone. I tried to run the codes given in above posts and it worked like a charm.
I ran the following code using my browser
$var = exec("g++ C:/wamp/www/cpp/hello.cpp -O3 -o C:/wamp/www/cpp/hello.exe");
echo $var;
The exe file is created. I am able to see the result when i run the exe file but the problem is when i run the above code in the browser, the result is not displayed on the webpage. I gave full access permission to all users but still give does not show the result on the webpage.
I really need help on this since i am doing a project on simulated annealing where i want to get the result from compiled c++ program and display it in the webpage with some jquery highcharts.
Thanks again to all, it has helped me alot and i have learnt alot as well.