PHP Start Windows Application, execute Bat batch, execute cmd command method (exec, System function detailed) _php instance

Source: Internet
Author: User

exec or system can invoke the cmd command

Directly on the code:

Copy Code code as follows:

<?php
/** Open the Windows Calculator * *
EXEC (' Start c:windowssystem32calc.exe ');

/** PHP to generate a batch file for Windows, and then execute 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 "Cannot open file $filename";
Exit
}

/** first we need to make sure that the file exists and can be written.
if (is_writable ($filename)) {

/** that's where $somecontent going to write when we use Fwrite ()
Write the $somecontent to the file we opened. */
if (fwrite ($handle, $somecontent) = = FALSE) {
echo "cannot be written to the file $filename";
Exit
}
echo "successfully writes $somecontent to the file $filename";
Fclose ($handle);
} else {
echo "File $filename not writable";
}
EXEC ($filename);
?>

There is a legacy problem, is the exec () call, PHP will always execute until you close the startup application, which will cause PHP execution timeout, do not know how to solve the problem, I hope the master passed here, leaving the answer! I'll fix it later, and it will be updated here!

The following from the information

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

PHP's built-in function Exec,system can invoke system commands (shell commands) and, of course, passthru,escapeshellcmd functions.

In many cases, using functions such as PHP's Exec,system to call system commands can help us get things done faster and better.

Note: In order to use the security mode in the two functions php.ini must be closed, otherwise PHP is not allowed to invoke system commands for security purposes.

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

EXEC---Execute an external program

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

Description
EXEC () executes the command commands given, but it does not output anything, it simply returns the last line from the result of the command, and if you need to execute a command and get all the data from the command, you can use the PassThru () function.
If a parameter array is given, the specified array is filled with each row that the command outputs. Note: If the array has previously contained elements, EXEC () will append it to the array, if you do not want the function to append elements, you can pass this array to exec () Call Unset () before.
If a parameter array and Return_var are given, the state command to return execution will be written to this variable.

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

Note: If you use this function to start a program and you want to leave it in the background (background), you must make sure that the output of the program is turned (redirected) to a file or some output stream, otherwise PHP will hang (hang) Until the program execution is finished.

System---Execute external programs and display output

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

Description

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

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

Note: If you use this function to start a program and you want to leave it in the background (background), you must make sure that the output of the program is turned (redirected) to a file or some output stream, otherwise PHP will hang (hang) Until the program execution is finished.
If PHP is operating as a server module, System () will try to automatically clear the output buffer of the Web server after each row is output.

Success is returned to the last line of the command, and false is returned.

If you need to execute a command and get all the data from the command, you can use the PassThru () function.

These two are all used to invoke the system shell command,

Different points:

EXEC can return all the results of execution to the $output function (array), $status is executed state 0 for success 1 for failure

Systerm does not need to provide the $output function, he is directly returning the result, same $return_var is executing the status code 0 for success 1 for failure

exec Example:

Copy Code code as follows:

<?php
$a = exec ("dir", $out, $status);
Print_r ($a);
Print_r ($out);
Print_r ($status);
?>

System Example:
Copy Code code as follows:

<?php
$a = System ("dir", $status);
Print_r ($a);
Print_r ($status);
?>

It says it looks a bit messy, and after running two examples, you'll understand!

"System"

Copy Code code as follows:

<?php
Set_time_limit (0);
Define (' Root_path ', DirName (__file__));

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

$cmdTest = ' Ps-ef | grep magent ';

$lastLine = System ($cmdTest, $retVal);

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

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

Output:

Copy Code code as follows:

++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:28:52
$lastLine
++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:28:52
Root 5375 5373 0 16:28 pts/1 00:00:00 grep magent
++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:28:52
$retVal
++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:28:52
0

"Exec"

Copy Code code as follows:

<?php
Set_time_limit (0);
Define (' Root_path ', DirName (__file__));

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

$cmdTest = ' Ps-ef | grep magent ';

$lastLine = EXEC ($cmdTest, $output, $retVal);

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

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

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

Output:

Copy Code code as follows:

++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:25:00
$lastLine
++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:25:00
Root 5360 5358 0 16:25 pts/1 00:00:00 grep magent
++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:25:00
$output
++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:25:00
Array
(
[0] => root 2838 1 0 15:39? 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 16:25 pts/1 00:00:00 sh-c ps-ef | grep magent
[2] => root 5360 5358 0 16:25 pts/1 00:00:00 grep magent
)

++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:25:00
$retVal
++++++++++++++++++++++++++++++++++++++++++
2014-10-15 16:25:00
0

Conclusion:

If you need detailed output results, then use EXEC ()! I generally use EXEC () to execute external commands!

Reference:

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.