Use the command line tool in PHP

Source: Internet
Author: User
Tags call shell php cli
If you have used PHP, you will find it an excellent tool for creating web pages with rich features. As a major scripting language, PHP is easy to learn. There are many powerful frameworks (such as CakePHP and CodeIgniter) that allow you to be as efficient as Rails programmers. With MySQL, PostgreSQL, and MicrosoftSQLServer,

If you have used PHP, you will find it an excellent tool for creating Web pages with rich features. As a major scripting language, PHP:

· Easy to learn.

· There are many powerful frameworks (such as CakePHP and CodeIgniter) that allow you to be as efficient as Rails programmers.

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

· Easy integration with JavaScript frameworks, such as script. aculo. us and jQuery.

But sometimes you want to do more things, or you must do more things. I mean you have to deal directly with the file system of the server running PHP. You finally need to process the files on the file system to understand the running processes or execute other tasks.

First, you are satisfied with the use of the file () command in PHP to open the file. However, to some extent, the only way to accomplish something is to run shell commands on the server and obtain specific output. For example, you may want to know how many files are contained in a specific directory. Or you want to know how many lines have been written to a group of log files. Or you want to operate 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 any Web browser. In this article, I will look at the same theme from another perspective and show you how to closely integrate with the underlying shell commands and include the returned values in your interface and process.

These operations are valid only when you run on Linux, Berkeley Software Distribution (BSD), or some other UNIX versions. I assume that you are running on the Linux-Apache-MySQL-PHP (LAMP) stack. If you run UNIX of another version, the details may be different, because the command line availability varies in each version. I know that many people are still engaged in development on Mac OS x (running a certain version of BSD), so I try my best to ensure the versatility of the sample commands and ease of transplantation.

  Command line overview

PHP Command Line Interface (CLI) Server Application Programming Interface (SAPI) is released in PHP V4.2.0 for testing purposes. To V4.3.0, it is fully supported and enabled by default. Php cli sapi allows you to develop shell scripts supported by PHP, or even desktop-based scripts. In fact, you can use PHP to create a tool that can 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, allowing you to understand the underlying shell environment and file system running PHP. PHP provides a large number of functions for executing external commands, including shell_exec (), exec (), passthru (), and system (). These commands are similar, but provide different interfaces for the external programs you run. All these commands are derived from a sub-process used to run your specified commands or scripts, and each sub-process will capture them when the command output is written to the standard output (stdout.

  Shell_exec ()

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.

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:

2017165readme.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 desired result. Suppose 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 shown 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 shell and then return results in the form of strings.

Figure 1. result of running shell command through shell_exec ()

Note that only the suffix marker operator will get the same result, as shown below.

Listing 3. only use the suffix marker operator

  

$ Results = 'WC-w *. txt ';

Echo $ results;

?> $ Results = 'WC-w *. txt ';

Echo $ results;

>

Listing 4 provides a simpler method.

Listing 4. simpler methods

  

Echo 'WC-w *. txt ';

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

>

It is important to know that many things can be done through UNIX command lines and shell scripts. For example, you can use a vertical line to connect commands. You can even use operators to create shell scripts in them and only call shell scripts (use or not use parameters as needed ).

For example, if you only want to calculate the number of words in the first five text files in the directory, you can use a vertical line (|) to connect the wc and head commands. In addition, you can place the output result in the pre tag so that it can be visually displayed in a Web browser, as shown below.

Listing 5. more complex shell commands

  

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.