Using the command-line tool in PHP _php tutorial

Source: Internet
Author: User
Tags php cli sapi perl script
If you've ever used PHP, you'll find it a great tool for creating rich Web pages. As a large scripting language, PHP:

• Easy to learn.

• There are many powerful frameworks (such as CakePHP and CodeIgniter) that enable you to be as efficient as a Rails programmer.

• Ability to communicate with MySQL, PostgreSQL, Microsoft SQL Server, and even Oracle.

• Easily integrate with JavaScript frameworks, such as script.aculo.us and JQuery.

But sometimes, you want to do more things, or you have to do more things. I mean you have to work directly with the file system of the server where PHP is running. You eventually need to work with files on the file system to understand the running process or perform other tasks.

First, you are satisfied with opening the file in PHP by using file () command. But to some extent, the only way to accomplish something is to run the shell command on the server and get a specific output. For example, you might want to know how many files a particular directory contains. Or you want to know how many lines of content have been written to a set of log files. Or you want to manipulate these files, copy them to another directory, or use rsync to send them to another location.

In the PHP command line? Yes, you can! In this article, Roger McCoy demonstrates how to use php--directly from the command line without requiring any Web browser. In this article, I look at the same topic from another perspective, showing you how to integrate tightly with the underlying shell commands, and include the return values in your interfaces and processes.

These actions are only valid if you are running on Linux, Berkeley software Distribution (BSD), or some other UNIX version. I assume that you are running on the linux-apache-mysql-php (LAMP) stack. If you run other versions of UNIX, the specifics may be different because the command line availability is different in each version. I know a lot of people are still working on Mac OS X (which runs a version of BSD), so I try to keep the sample commands as versatile as possible to ensure portability.

  Command line overview

PHP Command Line Interface (CLI) Server application Programming Interface (SAPI) was launched in PHP V4.2.0 for experimental purposes. To V4.3.0, it has been fully supported and enabled by default. PHP CLI SAPI allows you to develop PHP-supported shell scripts, even desktop-based scripts. In fact, PHP can be used to create tools that can be run directly from the command line. In this way, PHP developers can be as efficient as Perl, AWK, Ruby, or shell programmers.

This article explores the tools built into PHP to give you an idea of the underlying shell environment and file system that PHP is running. PHP provides a number of functions for executing external commands, including shell_exec (), exec (), PassThru (), and System (). These commands are similar, but provide different interfaces for external programs that you run. All of these commands derive a subprocess that runs the commands or scripts that you specify, and each child process captures them when the command output is written to standard output (stdout).

  Shell_exec ()

The shell_exec () command line is actually only a variant of the anti-apostrophe (') operator. If you have written a shell or Perl script, you know that you can capture the output of other commands inside the anti-apostrophe operator. For example, listing 1 shows how to use the backslash to get the word count for each text (. txt) in the current directory.

Listing 1. Use a backslash to calculate the number of words

#! /bin/sh

number_of_words= ' wc-w *.txt '

Echo $number _of_words

#result would is something like:

#165readme. txt 388results.txt 588summary.txt

#andso on ....

In your PHP script, you can run this simple command in Shell_exec (), as shown in Listing 2, and get the results you want. This assumes that there are some text files in the same directory.

Listing 2. Run the same command in Shell_exec ()

  

$results =shell_exec (wc-w *.txt);

Echo $results;

?> $results =shell_exec (wc-w *.txt);

Echo $results;

>

As you can see in Figure 1, the results are the same as those obtained from the shell script. This is because Shell_exec () allows you to run external programs through the shell and then return the results as strings.

Figure 1. The result of running the shell command through Shell_exec ()

Note that using the post-apostrophe operator will also get the same result, as shown below.

Listing 3. Use only the post-apostrophe operator

  

$results = ' wc-w *.txt ';

Echo $results;

?> $results = ' wc-w *.txt ';

Echo $results;

>

Listing 4 shows a simpler approach.

Listing 4. A simpler approach

  

Echo ' wc-w *.txt ';

?> Echo ' wc-w *.txt ';

>

It is important to know that there are many things that can be done with UNIX command line and shell scripts. For example, you can use a vertical bar to connect commands. You can even use the operator to create a shell script in it, and call only the shell script (using or not using parameters as needed).

For example, if you only want to calculate the number of words for the first 5 text files in that directory, you can use a vertical bar (|) to connect the WC and head commands. In addition, you can put the output inside the pre tag so that it can be rendered more aesthetically in a Web browser, as shown below.

Listing 5. More complex shell commands

  

http://www.bkjia.com/PHPjc/486464.html www.bkjia.com true http://www.bkjia.com/PHPjc/486464.html techarticle If you've ever used PHP, you'll find it a great tool for creating rich Web pages. As a big scripting language, PHP: Easy to learn. There are many powerful frameworks (such as ...

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