Differences and usage of calling external command functions system, exec, passthru, escapeshellcmd, shell_exec in php

Source: Internet
Author: User
Php provides the system (), exec (), passthru (), shell_exec () functions to call external commands. although these commands can execute shell commands in linux, they are actually different: system () outputs and returns the last shell result. Exec () does not output results. The last shell result is returned. all results can be saved to a returned array. P php provides the system (), exec (), passthru (), shell_exec () functions to call external commands. although these commands can execute shell commands in linux, they are actually different:

System () outputs and returns the last shell result.

Exec () does not output results. The last shell result is returned. all results can be saved to a returned array.

Passthru () only calls the command and directly outputs the command running result to the standard output device.

The shell_exec () command line is actually only a variant of the anti-apostrophes (') operator. If you have written shell or Perl scripts, you can capture the output of other commands in the anti-float operator.

Similarities: both of them can obtain the command execution status code.

Escapeshellcmd () removes special characters from the string. It can prevent users from attracting hackers to crack the server system.

System ()
Prototype: string system (string command [, int return_var])
The system () function is similar to other languages. 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.
Example:
1
System ("/usr/local/bin/webalizer ");

Exec ()
Prototype: string exec (string command [, string array [, int return_var])
The exec () function is similar to system (). It also executes the given command, but does not output the result, but returns the last line of the result. Although it only returns the last line of the command result, the complete result can be obtained using the second parameter array by appending the result row by row to the end of the array. If the array is not empty, we recommend that you use unset () to clear it before calling it. Only when the second parameter is specified can the third parameter be used to obtain the command execution status code.
Example:
1
Exec ("/bin/ls-l ");
2
Exec ("/bin/ls-l", $ res );
3
Exec ("/bin/ls-l", $ res, $ rc );

Passthru ()
Prototype: void passthru (string command [, int return_var])
Passthru () only calls the command and does not return any results, but directly outputs the running result of the command to the standard output device as is. Therefore, the passthru () function is often used to call programs such as pbmplus (a Unix-based image processing tool that outputs binary original image streams. It can also get the status code of command execution.
Example:
View sourceprint?
1
Header ("Content-type: image/gif ");
2
Passthru ("./ppmtogif hunte. ppm ");

Shell_exec ()
Prototype: string shell_exec (string $ cmd)

The shell_exec () command line is actually only a variant of the anti-apostrophes (') operator. If you have written shell or Perl scripts, you can capture the output of other commands in the anti-float operator. For example, listing 1 shows how to use the extension number to count the words in each vertex (.txt) in the current directory.

PHP command line overview

List 1. calculate the number of words using the anti-marker

#! /Bin/sh
Number_of_words = 'WC-w *. txt'
Echo $ number_of_words

# Result wocould be something like:
#165 readme.txt 388 results.txt 588 summary.txt
# And so on ....
In your PHP script, you can run this simple command in shell_exec (), as shown in listing 2, and get the desired result. Suppose there are some text files in the same directory.

Listing 2. run the same command in shell_exec ()

12 Next page

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.