The use of EXEC function in PHP to solve the idea

Source: Internet
Author: User
Tags php web development sendmsg
The use of the EXEC function in PHP
Wrote a PHP script, that is, to run an application with exec, the code is as follows:
PHP Code
  
   
  
   

However, there is no response when accessing this PHP page in the browser, $return _array display is empty. If I enter C:\wamp\www\FlowServer\sendmsg\test\bin\Debug\test.exe on the cmd command line, the program will run normally. Why is this?

------Solution--------------------
Http://topic.csdn.net/u/20070627/10/39bd28ed-05e7-41b1-984d-f205ee4672ac.html
------Solution--------------------
Four ways to execute system external commands using PHP
April 14, 2010 reads: 3,063 views
Read the comments and leave a comment

In the php file function about how to delete the file instance tutorial, I mentioned that there are two ways to delete a file using the Unlink function, and one is to execute the delete file command by invoking the system command through the systems function, in fact, in addition to the system function, PHP executes the command in many ways, Here are the four ways to execute commands that are most commonly used in PHP web development.

Preparatory work

For security reasons, the PHP runtime environment, such as virtual host, XAMPP, Dedeampz, and so on, is usually forbidden to invoke external commands from the system. So when you need to call the system external commands using PHP's function to execute commands, you need to make sure that the PHP runtime environment supports PHP's function of executing commands.

If you are using XAMPP, Dedeampz, the general default is to prohibit the execution of PHP command functions, you can disable_functions in the php.ini configuration file to remove the function you want to use, and then restart Apache. I'm using Dedeampz, so I'm going to go beyond the four functions of exec (), PassThru (), System (), Shell_exec (). How is the PHP operating environment configured?

If your PHP runtime is self-configuring, the default php.ini configuration file does not prohibit you from calling functions that execute external commands, but for security reasons, you should also disable the invocation of a part of the function that executes the external command of the system, and find Disable_functions, configured as follows:

Disable_functions = Exec,system,passthru,shell_exec

This disables the execution of these three functions, separating each function with a comma. Php. INI How to configure?

If you are using a virtual host, you need to ask the host provider whether the PHP environment supports functions that use PHP to execute commands.

Other Notes:

Since I am using a Windows system, the example tutorial to invoke the Windows system external commands mainly, if you are familiar with the Linux operation, you may wish to execute Linux commands under the Linux system.

Four ways to execute commands in PHP

Method One: Use the EXEC function to execute system external commands

Prototype: function exec (string $command, array[optional] $output, int[optional] $return _value)
1
2
3
4

EXEC ("dir", $outPut);
Print_r ($outPut);
?>

Description: Lists all directory and file information in the same directory as the PHP executable file.

Knowledge Point: Exec executes the system external command without outputting the result, but instead returns the last line of the result, and if you want the result you can use the second parameter to output it to the specified array, where one record represents the output line, that is, if the output has 20 rows, the array will have 20 records. So if you need to repeatedly output the results of calling external commands from different systems, you might want to clear this array when outputting the result of each system external command, in case of confusion. The third parameter is used to get the status code for the execution of the command, and usually the success is returned 0.

Method Two: Use the system function to execute the external command

Prototype: function System (String $command, int[optional] $return _value)
1
2
3

System ("dir");
?>

Knowledge Points: The difference between system and exec is that the system outputs the results directly to the viewer when executing the external commands of the systems, and returns true if the execution command succeeds, otherwise false. The second parameter has the same meaning as the third parameter of exec.

Method Three: Use function PassThru to execute system external command

Prototype: function PassThru (string $command, int[optional] $return _value)

Knowledge Points: The difference between PassThru and system, passthru directly outputs the result to the viewer, does not return any values, and it can output binary, image data.

Method Four: Reverse apostrophe ' (and ~ in the same key) Execute system external command
1
2
3

Echo ' dir ';
?>

Knowledge Points: When you use this method to execute system external commands, you want to make sure that the Shell_exec function is available, otherwise the system external commands cannot be executed using this type of anti-apostrophe.

Security instructions

When you use these functions to execute commands, you need to consider system security if you are committing the data according to user submissions, and you can use the Escapeshellcmd () and Escapeshellarg () functions to prevent malicious users from executing commands on the system. Escapeshellcmd () is for system commands executed, while Escapeshellarg () is for parameters that execute system commands. These two parameters are somewhat similar to the functionality of Addslashes ().

Other Notes

When the result of the execution of a command is very large, you may want to consider outputting the returned result to another file, and then read the file separately, which can significantly improve the efficiency of program execution. That
1
2
3

System ("dir > Leapsoulcn.txt");
?>

Note: Here the system executes the command without outputting the result directly to the viewer but outputting it to a file in the specified directory, which significantly increases the efficiency of the program execution.

At this point, the four most common methods of executing commands in PHP are exec (), PassThru (), System (), Shell_exec (), and the differences between them, and it is useful to use these functions to execute system external commands in the context of PHP web development.

Note: PHP Web development tutorial-leapsoul.cn All rights reserved, please use the link form to indicate the original source and this statement, thank you.


------Solution--------------------
PHP Code
$sendmsg = "C:\\wamp\\www\\flowserver\\sendmsg\\test\bin\\debug\\test.exe";
  • 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.