Using command line tools in PHP

Source: Internet
Author: User
Tags file system php cli php script sapi perl script

If you've ever used PHP, you'll find it an excellent tool for creating feature-rich Web pages. As a big scripting language, PHP: Easy to learn.

There are a number of powerful frameworks (such as cake and CodeIgniter) that allow you to be as efficient as Rails programmers.

Able to communicate with MySQL, PostgreSQL, Microsoft®sql Server, and even Oracle.

Can 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. I mean you have to deal directly with the file system of the server that PHP is running on. You eventually need to work with files on the file system to understand which processes are running or to perform other tasks.

First, you are satisfied with opening files in PHP using the file () command. But in a way, 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 are written to a group 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 any Web browsers. 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 value in your interface and process.

These actions are valid only if you are running on linux®, Berkeley Software distribution (BSD), or some other UNIX® version. I assume you run on the linux-apache-mysql-php (LAMP) stack. If you run another version of UNIX, the specifics may be different, because the command line's availability is different in each version. I know that a lot of people are still working on Mac OS X (running a certain version of BSD), so I try to keep the versatility of the example commands to make sure the porting is convenient.

Command line overview

PHP Command Line Interface (CLI) Server application Programming Interface (SAPI) is launched in PHP V4.2.0 for experimental purposes. When you go to V4.3.0, you are fully supported and enabled by default. The PHP CLI SAPI allows you to develop PHP-supported shell scripts, even desktop based scripts. In fact, you can use PHP to create tools that you 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 to help you understand the underlying shell environment and file system that PHP is running on. PHP provides a number of functions for executing external commands, including shell_exec (), exec (), PassThru (), and System (). These commands are similar, but provide a different interface for the external programs that you run. All of these commands derive a subprocess to run the command or script that you specify, and each subprocess catches 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 reverse-apostrophe (') operator. If you have written a shell or Perl script, you know that you can capture the output of other commands within the reverse-apostrophe operator. For example, listing 1 shows how to use the reverse apostrophe to get a word count for each text (. txt) in the current directory.

Listing 1, using an inverse apostrophe to calculate the number of words

#! /bin/sh
number_of_words=`wc -w *.txt`
echo $number_of_words
#result would 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. This assumes that there are some text files under the same directory.

Listing 2, running the same command in Shell_exec ()

<?php
$results = shell_exec('wc -w *.txt');
echo $results;
?>

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

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

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.