How PHP launches Windows applications, executes bat batches, executes CMD commands (exec, system functions in detail), Exec functions _php tutorial

Source: Internet
Author: User

How PHP launches Windows applications, executes bat batches, executes CMD commands (Exec, system function details), EXEC functions


exec or system can invoke cmd command

Directly on the code:

Copy the Code code as follows:
<?php
/** Open the Calculator for Windows */
EXEC (' Start c:windowssystem32calc.exe ');

/** PHP generates a batch file for Windows, 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 want to make sure that the file exists and is writable */
if (is_writable ($filename)) {

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

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

The following information is from

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

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

In many cases, using PHP's Exec,system and other functions to call system commands can help us do our job better and faster.

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

Let's take a look at the explanations of these two functions in the PHP manual:

EXEC---Execute external program

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

Description
EXEC () Executes the command 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 will be filled with each line output by the command, note that if the array already contains some elements, exec () will append it to the array, and if you do not want this function to append elements, you can pass this array to exec () Before calling Unset ().
If there is a given parameter array and Return_var, then the return execution Status command will be written to this variable.

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

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

The system---Executes the external program and displays the output

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

Description

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

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

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

Success returns the last line of the command, which returns false if it fails.

If you need to execute a command and get all the information 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 execution results to the $output function (array), $status is the execution status 0 for success 1 for failure

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

exec Example:
Copy the Code code as follows:
<?php
$a = exec ("dir", $out, $status);
Print_r ($a);
Print_r ($out);
Print_r ($status);
?>

System Example:
Copy the Code code as follows:
<?php
$a = System ("dir", $status);
Print_r ($a);
Print_r ($status);
?>

The above description looks a bit messy, after running two examples, you will understand!

"System"

Copy the 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 the 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 the 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 the 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, then use EXEC ()! I usually use EXEC () to execute external commands!

Reference:

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


How do I execute a cmd command in C + + without the system function

Write your command in Notepad and save it as a batch file (suffix. bat).
Run the batch with the WinExec function

How PHP executes cmd commands or bat processing-technical questions

The function is with these two exec (); system (); No, it should be your command to write wrong. $str =null;exec (\ "dir c: \", $STR);

http://www.bkjia.com/PHPjc/897017.html www.bkjia.com true http://www.bkjia.com/PHPjc/897017.html techarticle PHP launches the Windows application, executes the bat batch, executes the cmd command (exec, the system function in detail), the EXEC function in detail exec, or the system can invoke the cmd command directly to the ancestors ...

  • 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.