PHP Shell language

Source: Internet
Author: User
Tags ftp connection php database
In the past one or two years, the Lenovo software Hyperstar studio, PersonalHypertextPreprocessor, seems to have become the most widely used web page processing language in Linux/Unix. it is convenient, powerful, and open source code (OpenSource) as a result, it is gradually occupying the traditional CGI, and even the MicroSoftASP (ActiveServerPage) Lenovo software Chaoxing Kong studio Ma Hongzhi

PHP (Personal Hypertext Preprocessor) has become the most widely used web page processing language in Linux/Unix in the past one or two years. it is convenient, powerful, and open source code (OpenSource) it is gradually occupying the traditional CGI, or even the MicroSoft ASP (Active Server Page) market. many web sites use PHP for dynamic web Page processing.

The reason can be summarized as follows:

PHP is an OpenSource software based on the GPL protocol. it is open source code, free to use, and free to distribute. This attracts a large number of users and is also supported by commercial companies, especially in PHP4, zend provides the engine and optimization part for PHP, and you can access http://www.php.net or http://www.zend.com to get more information.

PHP syntax is very simple, and it is very similar to C and Perl. When I first came into contact with PHP, I felt like it was a clone of Perl. PHP itself is very easy to understand, and provides object-oriented processing capabilities, so that new users can learn in the shortest time.

PHP provides rich functions, including mathematical processing, string processing, network-related functions, various database support, and image processing functions. Many people provide a variety of new features for PHP development, and it has excellent scalability. Currently, PHP provides better support for Flash.

PHP is easily combined with Apache and can be used as an Apache Module. at the same time, it is quite simple to set and install in Apache, because Apache currently occupies 60% of the global market of Web Server, PHP naturally becomes the best combination of Apache. At the same time, to speed up the web server, PHP can be directly compiled into Apache, improving the access and processing speed of web services.

Another function of PHP is that it can be applied as a shell script. PHP as a shell script has great advantages over other shell languages. In particular, PHP database service functions are very powerful, allowing users to easily access the database system.

Compile PHP to make it an interpreter. the operating environment in this article is HappyLinux home version V1.0. perform the following steps as root:


Obtain PHP source code

PHP source code on the http://www.php.net site can be obtained, while many of the domestic site to provide improved site images. Currently, the latest version is PHP 4.0.2.


Unlock the PHP source code package

Tar zxvf php-4.0.2.tar.gz


Compile PHP

Cd php-4.0.2

Configure -- with-ftp -- with-pgsql

Make

Pay attention to the configure parameters to view the PHP help file, so that you can better configure PHP.


After compilation is complete, a php file is generated under the php-4.0.2 directory and copied to/usr/sbin/

Cp./php/usr/sbin


Test compilation result

Enter the following result to the test. php file:

#! /Usr/sbin/php


Echo "Hello World! \ N"

?>

Save the result to test. php and change the attributes of the file:

Chmod 777./test. php

Run the test. php file.

./Test. php

Then you can see the result:

X-Powered-By: PHP/4.0.2

Content-type: text/html

Hello World!

You may be disappointed with this result. it is not all we need. Because PHP is mainly used as a web application, and the first two lines above are the output HPPT headers. how can we remove these two lines from PHP? You just need to add the parameter-q to remove the HPPT header:

#! /Usr/sbin/php-q


Echo "Hello World! \ N"

?>

Let's take a look at the output:

Hello World!


OK! Done. Below we will use PHP as a script to list some applications


Connect to the database using PHP

PHP provides a large number of database functions. PHP provides a set of database functions for almost all databases. Therefore, it is a good idea to use PHP to write scripts and access the database. The following example shows how to access the postgresql database using PHP:

#! /Usr/sbin/php-q


String = $ con = pg_Connect ("dbname = test port = 5432 host = 127.0.0.1 user = S \ password = S ");

Echo $ con. "\ n ";

Pg_exec ($ con, "insert into test1 values ('OK', 1, 1 )");

Pg_close ($ con );

?>

Execute this script. PHP accesses the database test and inserts a record into the table test1. For PHP database access content, see the relevant database functions and database documentation manual.


Use PHP for FTP automatic upload and download scripts

Php ftp functions are provided in PHP3. these functions can be used to automatically upload and download files. To use the FTP function provided by PHP, you must specify the parameter -- with-ftp when compiling PHP. The following is an example of FTP:

#! /Usr/sbin/php-q


// Set up basic connection

$ Conn_id = ftp_connect ("$ your_ftp_server ");


// Login with username and password

$ Login_result = ftp_login ($ conn_id, "$ ftp_your_name", "$ ftp_your_password ");


// Check connection

If ((! $ Conn_id) | (! $ Login_result )){

Echo "Ftp connection has failed! ";

Echo "Attempted to connect to $ your_ftp_server ";

Die;

} Else {

Echo "Connected to $ ftp_server, for your ";

}


// Upload the file

$ Upload = ftp_put ($ conn_id, "$ destination_file", "$ source_file", FTP_BINARY );


// Check upload status

If (! $ Upload ){

Echo "Ftp upload has failed! ";

} Else {

Echo "Uploaded $ source_file to $ your_ftp_server as $ destination_file ";

}


// Close the FTP stream

Ftp_quit ($ conn_id );

?>

Obviously, PHP can facilitate network file transmission, and there is no need to write a socket program.


Use PHP to process command line parameters

The similarities between PHP and Perl are that they process the same parameters. they provide variable arrays and variable counters: $ argv [] and $ argc. The following example shows an example of printing a command line string and the command line sum.

Test. php


#! /Usr/sbin/php-q


Echo $ argv [1]; // note that the first variable of $ argv [] starts from 1.

?>

Run:./test. php LegendSoftware in the command line.

Will print LegendSoftware


Test1.php


#! /Usr/sbin/php-q


$ Sum = 0;

For ($ I = 1; $ I <= $ argc; $ I ++)

$ Sum = $ sum + $ argv [$ I];

Echo $ sum;

?>

Run test1.php 1 2 3 4 5 and press enter to print 15.


Interaction in the Console

Many C programmers know that scanf functions are used for interaction in the Console. PHP is a language designed for the web, and users enter variables in the form for interaction. So how do I interact on the Console? The answer is simple. use the fopen function to open the system's standard input device for reading. The following program implements the function of reading strings from the standard input device:

#! /Usr/sbin/php-q


$ Fp = fopen ("/dev/stdin", "r ");

Echo "Please input a string :"

$ Inputstr = fgets ($ fp, 100 );

Fclose ($ fp );

Echo "This string is ";

Echo $ inputstr. "\ n ";

?>


Fgets ($ fp, 100) indicates that a maximum of 100 bytes can be read from the $ fp handle (that is, "/dev/stdin, executing this program will wait for our input. after we press enter, the program will print our input.

Summary

Based on the above introduction and my practical experience, PHP is very powerful, not only powerful in web processing, but also very convenient in processing scripts.

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.