PHP executes external commands on linux (sorting)

Source: Internet
Author: User
To call external commands in PHP, you can use, 1 & gt; to call specialized functions, 2 & gt; reverse quotation marks, 3 & gt; popen () to open the process, three methods 1. calling external commands in PHP describes how to call external commands in PHP. you can use them. 1> calling specialized functions, 2> Anti-quotation marks, 3> popen () function to open the process, three methods: Method 1: Use special functions provided by PHP (four): PHP provides four special functions for executing external commands: exec (), system (), passthru (), shell_exec () 1) exec () prototype: string exec (string $ command [, array & $ output [, int & $ return_var]) description: exec does not output results when executing system external commands, but returns the last line of results. If you want to get the result, you can use the second parameter to output it to the specified array. A record in this array represents the output row. That is, if there are 20 rows of output results, this array will have 20 records, so if you need to repeatedly output the results of calling different system external commands, it is best to clear this array unset ($ output) when outputting the results of each system external command to prevent confusion. The third parameter is used to obtain the status code for command execution. generally, 0 is returned for successful execution. 2) system () prototype: string system (string $ command [, int & $ return_var]) description: the difference between system and exec is that when system executes external system commands, it executes the given command, outputs and returns the result. The second parameter is optional and is used to obtain the status code after the command is executed. A brief introduction to the status code of the second parameter: if 0 is returned, it indicates that the operation is successful. in Bash, when an error occurs in a fatal signal, bash returns the 128 + signal number as the return value. If the command cannot be found, 127 is returned. If the command is found but cannot be executed, 126 is returned. Bash itself returns the return value of the last command. If an error occurs during execution, a non-zero value is returned. Fatal Signal: 128 + signoCan't find command: Could can't not execute: 126 Shell script successfully executed: return the last command exit statusFatal during execution: return non-zero 3) passthru () prototype: void passthru (string $ command [, int & $ return_var]) description: Difference between passthru and system. passthru directly outputs the result to the browser without returning any value, and it can output binary data, such as image data. The second parameter is an optional status code. 4) shell_exec () prototype: string shell_exec (string $ cmd) description: directly execute the command $ cmd $ Output";?> Method 2: Anti-apostrophes prototype: Anti-apostrophes '(and ~ Execute system external commands in the same key. Note: When using this method to execute system external commands, make sure the shell_exec function is available, otherwise, the system cannot run external commands using this anti-code. Method 3: Use the popen () function to open the process prototype: resource popen (string $ command, string $ mode) description: it can interact with commands. The method described earlier can only execute commands, but cannot interact with commands. Sometimes you have to enter something into the command. for example, when adding a system user, you need to call su to change the current user to the root user. The su command must enter the root password on the command line. In this case, the method mentioned previously cannot be used. The popen () function opens a Process Pipeline to execute a given command and returns a file handle that can be read and written to it. Returns a file pointer like the fopen () function. Unless you use a single mode to open (read or write), you must use the pclose () function to close it. This pointer can be called by fgets (), fgetss (), and fwrite. If an error occurs, FALSE is returned. & 1', 'r'); echo "'$ handle ';". gettype ($ handle ). "\ n"; $ read = fread ($ handle, 2096); echo $ read; pclose ($ handle);?> II. security: because PHP is basically used for WEB program development, security has become an important aspect of consideration. As a result, PHP designers added a security mode to PHP. In php. in ini, if safe_mode = On is run in safe mode, the PHP script will be restricted by the following four aspects: when opening a file, execute external commands to restrict the connection to the MySQL database. HTTP-based authentication. in security mode, only external programs in a specific directory can be executed, calls to other programs will be rejected. This directory can be specified by using the safe_mode_exec_dir command in the php. ini file, or by adding the-with-exec-dir option to compile PHP. the default value is/usr/local/php/bin. When you use these functions to execute system commands, you can use the escapeshellcmd () and escapeshellarg () functions to prevent users from maliciously executing commands on the system. escapeshellcmd () the command is executed, while the escapeshellarg () parameter is used to execute the system command. These two parameters are similar to the addslashes () function. III. timeout when a command is executed with a large number of returned results, you can consider outputting the returned results to other files and then reading the files separately, which can significantly improve the efficiency of program execution. If the command to be executed takes a long time, put the command in the background of the system to run it. However, by default, functions such as system () will not be returned until the command is run (in fact, the output result of the command is waiting), which will definitely cause the PHP script to time out. The solution is to redirect the command output to another file or stream, for example: /Tmp/abc ");?> However, it takes several minutes for me to call the dos command. in order to make it difficult for batch processing to write the result into the file, we need to execute the following program in sequence: PHP sets the time limit for calling system commands, if the call times out, although the command will still be executed, PHP does not get the return value and is terminated (the most hateful is that no error is displayed). modify php. ini and restart Apache to allow system commands to run for a longer time max_execution_time = 600 4. problems with running commands in a linux environment using PHP are generally executed as apache users, it may also be a www user who adds apache to the parent folder group that stores your files, and then changes the permission of this parent folder to 775. in this way, members of the owner group have the write permission, apache belongs to this group and can rewrite the permissions of all files in this directory. For example, the dirName directory like chown www: www dirName can be controlled by php. note: Changing the user method for running apache/php is not safe. even if the file or directory is already www, php security settings are also taken care of. some linux installation commands may still fail to run. for example, I have installed ffmpeg software because of linux running permission problems, even if ffmpeg has www permission settings, because the library files on which ffmpeg depends are not allowed to run by www users, php still reports errors 127 or 126 when running this program, you can use the ldd command to view the library information that the ffmpeg command depends on. In this case, you must configure the ffmpeg dependency Library. The specific method is a topic in linux management. I will not discuss it here.
Related Article

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.