Explains the example of PHP calling the FTP service for transmission

Source: Internet
Author: User
Tags ftp commands ftp protocol
During work, some projects do not use SVN and Mysql master-slave synchronization. Therefore, you need to manually package code, export SQL statements, and drag them to the local server every day. After a long time, I felt very complicated and began to think about how to be lazy.

During work, some projects do not use SVN and Mysql master-slave synchronization. Therefore, you need to manually package code, export SQL statements, and drag them to the local server every day. After a long time, I felt very complicated and began to think about how to be lazy.

Using php to implement code directories and database backup is relatively simple. you only need to use system functions to execute the corresponding linux commands. But how can I transfer the backup file to a local device? I first thought of FTP, the most widely used tool for transferring files on servers and locally.

There are roughly three methods to operate ftp using php:

1. execute ftp-related commands in linux using system functions

2. send underlying FTP commands through socket communication

3. use the FTP system functions of php

Among the three methods, the first two do not need to rely on the ftp extension of php, but you need to have a deep understanding of the underlying commands and corresponding values of the ftp protocol in linux commands or TCP/IP. If you are interested in the underlying commands and values of the ftp protocol, refer to http://www.php.net/manual/zh/function.ftp-get.php. In fact, the principle is the same as that of sending emails using the socket method. it connects to the corresponding port and sends the corresponding protocol commands. In addition, the underlying principle of using FTP system functions of php is described below. (I strongly recommend you try it. it is a very interesting process. when you have such experience, you will have a deep understanding of TCP/IP and web services)

Next, let's talk about how to use the FTP system function of php to complete the above operations.

Step 1: Remember to enable FTP extension.

Step 2: Learn about the http://www.php.net/manual/zh/ref.ftp.php (FTP related functions), it is very simple for students who have the foundation.

Part 3: Add code snippets

/*

* Function: package the code and SQL on 76 and upload it to the local machine.

* Author: Jiang Shen

* Time: 2012.08.31

*/

Define ("FILE_PATH", '/www /');

$ Today = date ("Y_m_d", time ());

// ================================================ ========================================================== ========

$ Code_name = FILE_PATH. 'allyes/adf /';

Using tar_name1_file_path.20.today.'.adf.tar ';

Unzip zip_name1_file_path.20.today.'.adf.zip ';

$ SQL _name = FILE_PATH. $ today. 'import. SQL ';

// ================================================ ========================================================== ========

$ Ftp_resource = ftp_connect ('10. 200.30.197 ', 21 );

// ================================================ ========================================================== ========

$ Is_login = ftp_login ($ ftp_resource, 'mysql100', '123 ');

If (! $ Is_login)

{

Echo 'logon failed ';

}

Else

{

$Is_file_ok1_ftp_put($ftp_resource,$today.'.adf.tar ', "$ tar_name", FTP_ASCII );

If ($ is_file_ OK)

{

Echo $ today. ".adf.tar transfer successful \ r \ n ";

}

Else

{

Echo $ today. ".adf.tar transmission failed \ r \ n ";

}

$Is_file_ok1_ftp_put($ftp_resource,$today.'.adf.zip ', "$ zip_name", FTP_ASCII );

If ($ is_file_ OK)

{

Echo $ today. ".adf.zip transfer successful \ r \ n ";

}

Else

{

Echo $ today. ".adf.zip transfer failed \ r \ n ";

}

$ Is_file_ OK = ftp_put ($ ftp_resource, $ today. 'import. SQL ', "$ SQL _name", FTP_ASCII );

If ($ is_file_ OK)

{

Echo $ today. ". import. SQL transfer successful \ r \ n ";

}

Else

{

Echo $ today. ". import. SQL transmission failed \ r \ n ";

}

}

?>

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.