PHP tutorial on how to use the shell script in PHP

Source: Internet
Author: User
Tags array error code log php script php tutorial rsync backup

As we all know, in computer science, the shell is similar to the Command.com under DOS. It receives the user command and then calls the appropriate application. At the same time it is a programming language. As a command language, it interprets and executes user-entered commands interactively or automatically interprets and executes predetermined series of commands; As a programming language, it defines variables and parameters, and provides many control structures that are available in higher-order languages, including loops and branches.

PHP after a long period of development, many users are very familiar with PHP, here I publish a personal understanding, and you discuss the discussion. In most cases, I use the exec () command and the data array to handle everything. or use Shell_exec () for simpler commands, especially if you don't care about the results. If you only need to return a PHP shell script, I'll use PassThru (). Usually, I use different functions in different situations, and sometimes they can be interchangeable.

It all depends on my mood and the purpose to be achieved. Another question you might ask is "what are their strengths?". If you don't have a clue, or if a project is good for using shell commands, but you don't know how to use them, I'm here to provide some insights. If you are writing an application that provides a variety of backup or file transfer features, you can choose to run the PHP shell script that rsync supports using either shell_exec () or one of the other commands provided here. You can write a PHP shell script that contains the necessary rsync commands, and then use PassThru () to execute it according to a user's command or cron job.

For example, a user who has the appropriate permissions (such as Administrator privileges) in your application wants to send 50 PDF files from one server to another. Then, the user needs to navigate to the correct location in the application, click Transfer, select the PDF you want to send, and then click Submit. In this process, the form should have a PHP script that uses the return option variable to run the Rsync script via PassThru (), so you know if the problem occurs, as shown below.

Listing 1. Example PHP script running rsync script via PassThru ()

<?php
PassThru (' xfer_rsync.sh ', $returnvalue);
if ($returnvalue!= 0) {
We have a problem!
Add error code here
}else{
We are okay
Redirect to some other page
}
?>

If your application needs to list processes or files, or data about those processes or files, you can use one of the commands summarized in this article to easily do this. For example, a simple grep command can help you find a file that matches a specific search condition. Use it with the EXEC () command to save the results in an array, which allows you to build an HTML table or form that further allows you to run other commands.

So far, I've discussed user-generated events--the user simply presses the button or clicks the link, and PHP runs the script. You can also use standalone PHP scripts with cron or other scheduling programs to achieve some interesting results. For example, if you have a backup script, you can run it through cron or package it into a PHP script and run it.

Why would you do that? It seems superfluous, isn't it? It's not--you need to consider that you can run the backup script through exec () or passthru (), and then perform some behavior based on the return code. If an error occurs, you can log it to the error log or database, or send a warning e-mail message. If the script succeeds, you can dump the original output to the database (for example, Rsync has a verbose (verbose) pattern that is useful for subsequent diagnostics).

Safety

Let's briefly discuss security here: If you accept user input and pass information to the shell, it's best to filter user input. Delete the commands and disallowed content that you think are harmful, such as sudo (run as Superuser) or RM (delete). In fact, you might not want users to send open requests, but instead let them choose from the list.

For example, if you run a transport program that accepts a list of files as a parameter, you should list all files through a series of check boxes. Users can select and deselect files and activate the rsync shell script by clicking Submit. Users cannot enter files themselves or use regular expressions.

This article describes the use of PHP shell scripts from two aspects, hoping to help you.







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.