PHP: how to start windows applications, execute bat batch processing, and execute cmd commands (exec, system function details) _ php instance

Source: Internet
Author: User
This article mainly introduces how to use PHP to start windows applications, execute bat batch processing, and execute cmd commands (exec and system functions). For more information, see Exec or system can call the cmd command.

Directly run the code:

The code is as follows:


<? Php
/** Open the windows calculator */
Exec ('Start C: WindowsSystem32calc.exe ');

/** Php generates a windows batch file and then executes this batch file */
$ Filename = 'T. bat ';
$ Somecontent = 'C:
';
$ Somecontent. = 'CD "C:/Program Files/MySQL-Front "';
$ Somecontent. ='
Start MySQL-Front.exe ';
If (! $ Handle = fopen ($ filename, 'w ')){
Echo "the file $ filename cannot be opened ";
Exit;
}

/** First, make sure the file exists and can be written */
If (is_writable ($ filename )){

/** That is where $ somecontent will be written when we use fwrite ().
Write $ somecontent to the open file. */
If (fwrite ($ handle, $ somecontent) === FALSE ){
Echo "cannot be written to file $ filename ";
Exit;
}
Echo "successfully writes $ somecontent to the file $ filename ";
Fclose ($ handle );
} Else {
Echo "file $ filename cannot be written ";
}
Exec ($ filename );
?>

There is a legacy problem, that is, the exec () call, php will continue to execute until you close the started application, which will cause php execution timeout, do not know how to solve this problem, hope the experts will pass by and leave the answer! I have solved the problem after the day and will update it here!

The following materials are from

========================================================== ==========

Php built-in functions exec and system can call system commands (shell commands). of course, there are also passthru, escapeshellcmd and other functions.

In many cases, using the exec, system, and other functions of php to call system commands can help us complete our work better and faster.

Note: to use these two functions, the security mode in php. ini must be disabled. otherwise, php will not allow you to call system commands for security reasons.

Let's take a look at the php Manual's explanation of the two functions:

Exec --- execute external program

Syntax: string exec (string command [, array & output [, int & return_var])

Note:
Exec () executes the command, but it does not output anything. it simply transmits the last line from the command result. if you need to execute a command, you can use the passthru () function to obtain all data from the command.
If the array parameter is given, the specified array will be filled with each line output by the command. note: If the array already contains some elements, exec () it will be appended to the back of the array. if you do not want this function to append an element, you can call unset () before passing this array to exec ().
If the array and return_var parameters are given, the status command returned for execution will be written to this variable.

Note: If you allow data input from the user to be passed to this function, you should use escapeshellcmd () to determine that this user cannot cheat (trick) system to execute arbitrary (arbitrary) commands.

Note: If you use this function to start a program and want to leave it when running in the background (background), you must ensure that the output of this program is redirected) to a file or some output data streams, otherwise PHP will be suspended (hang) until the program execution ends.

System --- execute an external program and display the output

Syntax: string system (string command [, int & return_var])

Note:

System () executes the command and outputs the result. If the return_var parameter is given, the status code for executing the command will be written to this variable.

Note: If you allow data input from the user to be passed to this function, you should use escapeshellcmd () to determine that this user cannot cheat (trick) system to execute arbitrary (arbitrary) commands.

Note: If you use this function to start a program and want to leave it when running in the background (background), you must ensure that the output of this program is redirected) to a file or some output data streams, otherwise PHP will be suspended (hang) until the program execution ends.
If PHP runs as a server module, after each line is output, system () will try to automatically clear the web server's output buffer.

If the command succeeds, the last line of the command is returned. if the command fails, false is returned.

If you need to execute a command and obtain all the information from the command, you can use the passthru () function.

Both are used to call the system shell command,

Differences:

Exec can return all the execution results to the $ output function (array). $ status indicates that the execution status 0 indicates success 1 indicates failure.

Systerm does not need to provide the $ output function. it directly returns the result. Similarly, $ return_var indicates that the execution status code 0 indicates that success 1 indicates failure.

Exec example:

The code is as follows:


<? Php
$ A = exec ("dir", $ out, $ status );
Print_r ($ );
Print_r ($ out );
Print_r ($ status );
?>


System example:

The code is as follows:


<? Php
$ A = system ("dir", $ status );
Print_r ($ );
Print_r ($ status );
?>

The above description looks messy. after running two examples, you will understand it!

[System]

The code is as follows:


<? Php
Set_time_limit (0 );
Define ('root _ path', dirname (_ FILE __));

Include ROOT_PATH. '/include/global. func. php ';

$ Shorttest = 'PS-ef | grep magent ';

$ LastLine = system ($ release test, $ retVal );

Write_log ('$ lastline ');
Write_log ($ lastLine );

Write_log ('$ retval ');
Write_log ($ retVal );
?>

Output:

The code is as follows:


++ ++
16:28:52
$ LastLine
++ ++
16:28:52
Root 5375 5373 0 00:00:00 pts/1 grep magent
++ ++
16:28:52
$ RetVal
++ ++
16:28:52
0

[Exec]

The code is as follows:


<? Php
Set_time_limit (0 );
Define ('root _ path', dirname (_ FILE __));

Include ROOT_PATH. '/include/global. func. php ';

$ Shorttest = 'PS-ef | grep magent ';

$ LastLine = exec ($ release test, $ output, $ retVal );

Write_log ('$ lastline ');
Write_log ($ lastLine );

Write_log ('$ output ');
Write_log ($ output );

Write_log ('$ retval ');
Write_log ($ retVal );
?>

Output:

The code is as follows:


++ ++
16:25:00
$ LastLine
++ ++
16:25:00
Root 5360 5358 0 00:00:00 pts/1 grep magent
++ ++
16:25:00
$ Output
++ ++
16:25:00
Array
(
[0] => root 2838 1 0? 00:00:00 magent-u root-n 51200-l 192.168.137.100-p 12001-s 192.168.137.100: 11211-B 192.168.137.100: 11212
[1] => root 5358 5356 0 00:00:00 pts/1 sh-c ps-ef | grep magent
[2] => root 5360 5358 0 00:00:00 pts/1 grep magent
)

++ ++
16:25:00
$ RetVal
++ ++
16:25:00
0

Conclusion:

If you need detailed output results, use exec! I usually use exec () to execute external commands!

Refer:

Http://php.net/manual/zh/function.system.php
Http://php.net/manual/zh/function.exec.php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.