how to open a new CMD window using PHP
In the last days i was tying to code something in PHP to popup a CMD windows and this CMD windows have a command like «ping google.com» and process it
i don’t need the PHP code to read the result , i only want it to run it i tried some thing like
but no result (i don’t know if it run it in background )
so i read this and i founded many ways but nothing worked My OS is windows and thanks alot for all 🙂
First up: your path is all wrong: C:\Windows should be C:\\Windows\\System32 and the System32/cmd.exe should be System32\\cmd.exe . Forward slashes should work on windows, too, but don’t mix them together, be consistent. Also: where will the PHP script run, and where do you want the prompt to show up?
I can’t, not yet. There’s not enough information to go on here. I don’t know how you’re planning to run this PHP script, and what you expect to see. If you run this code on your machine, simply doing exec(‘ping 8.8.8.8’); should open a cmd window anyway, whereas exec(‘start /B ping 8.8.8.8’, $status, $result); starts the process in the background, leaving you to process the output that’ll be assigned to $result , and the exit code is found in $status
Looking at your previous questions, you seem to think that people here will write code for you. That’s not what this site is about: either you learn to code, or you pay someone to do it for you. This isn’t a code-request site, it’s a problem solving Q&A site for people who are willing to do the work themselves
3 Answers 3
the answer is
i searched alot to find it :))
popen(‘start /D «‘.getenv(«PHPRC»).'» /B ‘ . $exec_string, «r»); to set the Working Directory, avoiding DLL loading issues
How about a real command prompt? Here is a project that allows PHP to obtain and interact dynamically with a real cmd terminal. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
//if you prefer Powershell, replace 'cmd' with 'powershell' $shellObj = \MTS\Factories::getDevices()->getLocalHost()->getShell('cmd'); $strCmd1 = 'ping -n 4 www.google.com'; $return1 = $shellObj->exeCmd($strCmd1);
The return will give you the command return OR error from cmd, just as if you sat at the console.
Furthermore, you can issue any command you like against the $shellObj, the environment is maintained throughout the life of the PHP script. So instead of bundling commands in a script file, just issue them one by one using the exeCmd() method, that way you can also handle the return and any exceptions.
Функции запуска программ
Открытые файлы с блокировкой (особенно открытые сессии) должны быть закрыты до выполнения программы в фоне.
Смотрите также
Эти функции также тесно связаны с оператором обратные кавычки («).
User Contributed Notes 40 notes
If you are chrooting php into enviornment that doesn’t have /bin/sh you can’t execute any command! Even when you call
mail() and it calls sendmail . well actually sendmail NEVER gets called because /bin/sh is not in the chroot!
SO in conclusion: YOU MUST HAVE /bin/sh TO EXECUTE SOMETHING.
VERY IMPORTNAT!
I’ve lost few days while find this, hope it helps someone.
The only syntax I found to work for the command portion of an an exec() call on a Win2K devel platform is:
$cmd = «\»path-to-exe\» args file-name»;
where ‘path-to-exe’ has escaped double quotes, args are in the standard format, and ‘file-name’ has any backslashes escaped.
$cmd = «\»C:\program files\winzip\wzunzip.exe\» -c C:\\temp\\uploaded_file.zip»;
Note that the backslashes are escaped in the uploaded_file name, but not in the path to call the Winzip executable. Go figure!
To clarify even more about what has been said in the last few posts:
«exec», «backticks», «system» and so on will fail on Windows 2003 by default.
You must modify the security on cmd.exe to give the user account IUSR-computername the necessary permissions which are at least read & execute.
For those who want to execute a .php script to run in the background, from a
.php web page.
exec(«php script.php parameters 2>dev/null >&- /dev/null &»);
Where.
— php is the path to your php script executer (php has to be specifically complied to be to do this)
— script.php is the script
— parameters are none or more parameters
— 2>dev/null redirects the stderr to a file
— — >&- switches off the stdout
— >dev/null redirects all other output to the file dev/null
— & direct script to run in background
This is the only way I managed to get it working (Linux & Apache, php 4x enviroment) . What is also great is that the process is forked so even though a user may close the browser that initiated the exec(), the process will still run to completion. And the dev/null file can be turned into a log.
What I found odd is that the script I was execing would run fine in the bg when executed from the command line, but not from the browser until I closed the stdin and stdout.
at LAST! tenacity pays off, days trying every little thing!
I didn’t want batch files. I’m trying to use PHP to get rid of batch files.
I didn’t want to call a file to parse the parameters to call a shell to call a file. I’ve got «systems» right now with batches tiered three and five deep.
I just wanted to run stuff.
CALL, tested on WinXP, will be testing on more OSes right away, in PHP4 and 5, with exec, system, works with popen, and passthru.
here is my lame sample function.
// CreateZip
function createzip ($target, $archive)
< $ziputil = "call \"c:\\Program Files\\7-zip\\7z.exe\"";
$archived = escapeshellarg($archive);
$targeted = escapeshellarg($target);
$shellcommand= $ziputil.» a -tzip «.$archived.» «.$targeted.»\n»;
// all of the below are working in Win XP Pro
passthru ($shellcommand);
exec ($shellcommand);
system ($shellcommand);
shell_exec ($shellcommand);
$proc= popen ($shellcommand, «r»); //$proc contains output
LONG PATH NAMES WITH SPACES IN THEM ON WINDOWS!
all in a big long concatenated command line with multiple quoted-filename parameters
Note on XP users: XP-Home edition does not allow to set rights directly on files and folders. You should use cacls command-line utility to do this.
cacls c:\windows\system32\cmd.exe /E /G IUSR_ADMIN2003:F
gives IIS user full access to cmd.exe (potential security hole!), so PHP can fork and execute external programs.
The note is about usage of exec() under windows.
In replay to k dot mitz dot NO_SPAM at att dot NO_SPAM dot net: why not enclose your $cmd in simple quotes instead?
Here is where i’m at right now (also TRYING to use exec() and passthru() on windows XP sp2):
$cmd = ‘»C:\my path with spaces\targetapp.exe» C:\mypathnospaces\targetfile.xxx’;
exec($cmd);
Or, you can put your script into your directory C:\my path with spaces\, and work with php directly from there:
$cmd = ‘targetapp.exe «C:\my other path with spaces\targetfile.xxx»‘;
exec($cmd);
The above also works, provided of course your script has the correct working directory.
________
But. In your cmd.exe, you can issue:
«C:\my path with spaces\targetapp.exe» «C:\my other path with spaces\targetfile.xxx»
Although the above works perfectly in the cmd, the following php script does NOT work on my system:
$cmd = ‘»C:\my path with spaces\targetapp.exe» «C:\my other path with spaces\targetfile.xxx»‘;
exec($cmd,&$content);
Not that it sends an error, it just does nothing, and you get nothing written into your $content array. Not that I do understand why.
________
As far as my few tryings go, the root of the problem lies in the fact that the command to execute has two passages enclosed inside double quotes. Another command which I’ve had working in the cmd but NOT with php is:
‘»C:\my path with spaces\Apache2\bin\Apache.exe» -w -n «Apache2» -k restart’
Maybe someone has a solution; I don’t (other than using chdir).
If you plan to start programs on the server that show message boxes (things that require OK from the server-side), or remain (like notepad.exe), and the exec-command seems to go into an deadly loop, then MAYBE your program has started, but you can’t see it. Because the web server runs as an system process, and it isn’t allowed to interact with the desktop.
To solve a part of the problem (to see the programs you execute), in the control panel in Windows, goto Administration->Services, right-click the server-service, goto Properties and on the second tab (login?) and check the box about allowing the service to interact with the desktop. Click OK. Restart the webserver, and MAKE SURE it is restarted for real (i.e. look in the task manager so the service goes _down_), otherwise the desktop-option won’t take.
Next phase would be to stop PHP from waiting for the processes to complete (this is what makes PHP «loop»). I solved this by creating a small Delphi-application that took the path to the file I wanted to execute and executed it by the WinExec API, which just starts the childprogram but doesn’t wait for it to complete = the program completes and the PHP.exe completes the script. Problem solved!
Delphi-snippet:
WinExec(PChar(),SW_SHOW); // replace with the program path.
I was stuck for about an hour and a half last night trying to get Perl to pass back values to my PHP script after running an exec() command.
Since my Perl script was using STDOUT (after being passed command lines variables), I expected the variables used in my .pl script to be accessible in PHP, no questions asked.
Of course, this wasn’t working and I finally figured it out with the help of a friend (Shawn = superstar): you need to echo the exec() command in order to get the values back into PHP.
Since I was returning multiple values, I added pipe delimiters in my .pl script and then used PHP to parse the string to retreive my data into different variables.
#hit the .pl script with the data
$mydata = exec(«script.pl ‘command line args’ «);
exec(«exit(0)»);
#parse the value that comes back from the .pl script
list($strA, $strB) = split (‘[|]’, $mydata);
echo «valueA: » . $strA.»
«;
echo «valueB: » . $strB.»
«;
In case you ever had to chain from php to another program (e.g. with a cgi php that only gives part of the output, or with php-gtk), here is a little C program that kills his parent (php, for instance), then launches a program given in argument.
int main(int argc, char**argv) /* kill the parent */
kill(getppid(), 15);
argv++;
/* then launch the new program */
return execvp(argv[0], argv);
>
(compile with gcc -O3 -o chain chain.c)
then in php, use
exec(‘chain sh -c echo test’);
?>
Just following up my previous post, in reply to lancelot—du-lac at hotmail dot fr:
The behaviour described was fixed in PHP 5.3 in the following commit:
To replicate the fix in your own code, so it also runs on PHP 5.2, instead of:
$cmd = «. any shell command, maybe with multiple quotes. » ;
exec ( $cmd );
?>
use
$cmd = «. any shell command, maybe with multiple quotes. » ;
exec ( $cmd );
?>
This replicates the behaviour of PHP 5.3 and above. Note that this applies to all shell commands (not just exec(), as used in my example).
Within Linux, when calling the php interpreter directly from popen, or other program execution function to execute a script, it appears as though the script perpetually fails and re-executes.
i.e.
As each re-execution causes the executing script to load under a new PID, it is incredibly difficult to identify and kill the process manually.
[One solution is] to ensure the executing script has a valid shebang, and execute permissions. This allows you to execute the script directlyI found this comment on this page:
[begin of quote]Trying to us the following code failed badly with various results: like «unable to fork», «access denied», «empty results», depending on what settings I used, . even though the same code worked from command line on the server itself.$retstr = exec(‘nslookup -type=mx myhost.com’, $retarr);
Instead of nslookup I believe this would apply to most programs from the \system32\ directory.
I had to learn that the following finally worked:
$retstr = exec(‘c:\php\safedir\nslookup -type=mx myhost.com’, $retarr);
. but only under the listed preconditions:
1: nslookup.exe is placed (copied) in the directory \php\safedir\
2: the directory \php\safedir\ is included in the system PATH environement variable
3: the file cmd.exe is placed in \php\ as listed by other notes above
4: the directory «c:\php\safedir\» is set in the php.ini setting
safe_mode_exec_dir = «c:\php\safedir\»
.. maybe set in php-activescript.ini as well, depending on your system setup.
5: nslookup is referenced by the full path as otherwise the file from \windows\system32\ will be called. This happend to me with empty result due to missing rights!
Hope this helps somebody saving some time and headaches.
[end of quote]
This is just to complicated. Only two things are needed:
1. Specific permissions for the IUSR account for read & execute to the cmd.exe in C:\Windows\System32 directory
2. Specific permissions for the IUSR account for read & execute to the command that’s needed (example: nslookup.exe in C:\Widnows\System23 directory)
With just this two conditions the exec works fine
(This is for an IIS server running on a windows platform)