Home Backend Development PHP Tutorial Using command line tools in PHP_PHP Tutorial

Using command line tools in PHP_PHP Tutorial

Jul 13, 2016 pm 05:38 PM
php web use create Discover Command Line exist tool you characteristic of

If you have used PHP, you will find that it is an excellent tool for creating feature-rich web pages. 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 productive as a Rails programmer.

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

·Ability to easily integrate with JavaScript frameworks such as script.aculo.us and jQuery.

But sometimes, you want to do more, or have to do more. What I mean is that you have to deal directly with the file system of the server where PHP is running. You will eventually need to work with files on the file system, learn about running processes, or perform other tasks.

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

 In the article "PHP Command line? Yes, you can!" Roger McCoy demonstrates how to use PHP - No web browser required. In this post, I look at the same topic from another angle, showing you how to tightly integrate with the underlying shell commands and include return values ​​into your interface and processes. These operations only work if you are running on Linux, Berkeley Software Distribution (BSD), or some other UNIX version. I'm assuming you're running on the Linux-Apache-MySQL-

PHP

(LAMP) stack. If you are running another version of UNIX, the specific details may be different because the availability of the command line is different in each version. I know many people are still developing on Mac OS X (running some version of BSD), so I tried to keep the example commands generic to ensure easy portability.

 

Command line overview  

PHP

Command Line Interface (CLI) Server Application Programming Interface (SAPI) was released in PHP V4.2.0 for experimental purposes. As of V4.3.0, it is fully supported and enabled by default. The PHP CLI SAPI allows you to develop PHP supported shell scripts, even desktop-based scripts. In fact, it is possible to use PHP to create tools that can be run directly from the command line. This way, PHP developers can be as productive as Perl, AWK, Ruby or shell programmers.  This article explores the tools built into

PHP

to let you understand the underlying shell environment and file system in which PHP runs. 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 you run. All these commands spawn a child process for running the command or script you specify, and each child process will write the command output to standard output ( stdout).  

shell_exec()

 shell_exec() The

command

line is actually just a variation of the backtick (`) operator. If you've ever written a shell or Perl script, you know that you can capture the output of other commands inside the backtick operator. For example, Listing 1 shows how to use backticks to get the word count for each text (.txt) in the current directory. Listing 1. Counting words using backticks

 #!/bin/sh

Number_of_words=`wc -w *.txt`

echo $number_of_words

 #result would be 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 desired results. It is assumed here that there are some text files in the same directory.

Listing 2. Running 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 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 returns the result as a string.

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

Note that just using the trailing apostrophe operator will give you the same result, as shown below.

Listing 3. Using only the trailing apostrophe operator

 

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

echo $results;

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

echo $results;

 >

Listing 4 shows a simpler approach.

List 4. A simpler method

 

echo `wc -w *.txt`;

?> echo `wc -w *.txt`;

 >

It’s important to know that a lot can be done with the UNIX command line and shell scripts. For example, you can use pipes to connect commands. You can even create a shell script in it using operators and just call the shell script (with or without arguments as needed).

For example, if you only want to count the number of words in the first 5 text files in the directory, you can use a vertical bar (|) to connect the wc and head commands. Alternatively, you can place the output inside a pre tag to render it more beautifully in a web browser, as shown below.

Listing 5. More complex shell Commands

 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486464.htmlTechArticleIf you have used PHP, you will find that it is an excellent tool for creating feature-rich Web pages. As a major scripting language, PHP: Easy to learn. There are many powerful frameworks (such as...

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to quickly build an efficient data query web application? How to quickly build an efficient data query web application? Apr 19, 2025 pm 10:24 PM

Efficiently build data query tools: Framework selection for simplifying the development process Product manager proposed a requirement to quickly build a data query tool, which requires...

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

Do Android development need to learn Kotlin? Do Android development need to learn Kotlin? Apr 19, 2025 pm 11:03 PM

Is Kotlin worth learning? Does Android development require Kotlin? Many Android developers will have questions when using Java for development: Kotlin language...

How to implement conditional control in the code to avoid unnecessary input operations? How to implement conditional control in the code to avoid unnecessary input operations? Apr 19, 2025 pm 10:30 PM

How to implement conditional control in the code to avoid unnecessary input operations? During the programming process, sometimes we need to control the code based on certain conditions...

When local characteristic project data is migrated to a unified construction system, how to ensure the smooth progress of the migration process? When local characteristic project data is migrated to a unified construction system, how to ensure the smooth progress of the migration process? Apr 19, 2025 pm 10:39 PM

Key considerations and implementation steps for migrating data from local special projects to a unified construction system When local special projects need to migrate data to a unified construction system...

The top ten virtual currency trading app addresses The top ten virtual currency trading app addresses Apr 21, 2025 am 09:24 AM

This article summarizes the information of the top ten virtual currency trading apps including Binance, Ouyi and Sesame Open Door, but for security reasons, the URL is not provided directly. Instead, it emphasizes the importance of safe access to the official platform through trusted channels and provides verification methods. At the same time, the article reminds investors to consider factors such as security, transaction fees, currency selection when choosing an APP, and pay attention to the risks of virtual currency trading.

A list of top 10 global leading virtual currency trading apps in 2025 A list of top 10 global leading virtual currency trading apps in 2025 Apr 21, 2025 pm 12:06 PM

The top ten leading virtual currency trading apps in the world in 2025 are: 1. Binance, 2. Gate.io, 3. OKX, 4. Huobi Global, 5. Bybit, 6. Kraken, 7. FTX, 8. KuCoin, 9. Coinbase, 10. Crypto.com.

Recommended top ten digital currency APPs in the world (authoritative release in 2025) Recommended top ten digital currency APPs in the world (authoritative release in 2025) Apr 21, 2025 pm 12:09 PM

The world's leading ten digital currency apps include: 1. OKX, 2. Binance, 3. Huobi, 4. Matcha (MXC), 5. Bitget, 6. BitMEX, 7. Pionex, 8. Deribit, 9. Bybit, 10. Kraken. These platforms have their own characteristics in security, transaction services, technical architecture, risk control team, user experience and ecosystem.

See all articles