Home >Backend Development >PHP Tutorial >PHP and FTP: How to transfer files through the command line

PHP and FTP: How to transfer files through the command line

王林
王林Original
2023-07-28 18:11:041608browse

PHP and FTP: Methods of file transfer through the command line

With the development of the Internet, file transfer has become one of the functions frequently used in daily work. FTP (File Transfer Protocol), as a traditional file transfer protocol, is still widely used. This article will introduce how to use PHP to implement FTP file transfer through the command line, and provide corresponding code examples.

1. Connect to the FTP server

Before starting the FTP file transfer, you first need to establish a connection with the FTP server. PHP has built-in FTP extension, which can realize the connection with simple code.

<?php
// 连接FTP服务器
$ftp_server = 'ftp.example.com';
$user_name = 'username';
$user_pass = 'password';

$ftp_conn = ftp_connect($ftp_server) or die("无法连接到服务器");
$login = ftp_login($ftp_conn, $user_name, $user_pass);

if (!$ftp_conn || !$login) {
    die("无法连接到FTP服务器或登录失败");
} else {
    echo "成功连接到FTP服务器";
}
?>

In the above code, we connect to the specified FTP server through the ftp_connect() function, and then use the ftp_login() function for login verification. If the connection and login are successful, prepare for file transfer.

2. Upload files

Uploading files is a common requirement for FTP file transfer. Below is a sample code for uploading files to an FTP server.

<?php
// 连接FTP服务器(略)

// 上传文件
$file_path = 'path/to/local/file.txt';
$remote_file = 'path/to/remote/file.txt';

if (ftp_put($ftp_conn, $remote_file, $file_path, FTP_ASCII)) {
    echo "文件上传成功";
} else {
    echo "文件上传失败";
}
?>

In the above code, we use the ftp_put() function to upload files. The $remote_file parameter specifies the path and file name stored on the FTP server after uploading, and the $file_path parameter specifies the path to the local file. FTP_ASCII as an optional parameter indicates uploading files in ASCII mode.

3. Download files

In addition to uploading files, downloading files is also a common operation for FTP file transfer. Below is a sample code for downloading a file from an FTP server.

<?php
// 连接FTP服务器(略)

// 下载文件
$remote_file = 'path/to/remote/file.txt';
$file_path = 'path/to/local/file.txt';

if (ftp_get($ftp_conn, $file_path, $remote_file, FTP_ASCII)) {
    echo "文件下载成功";
} else {
    echo "文件下载失败";
}
?>

In the above code, we use the ftp_get() function to download the file. The $remote_file parameter specifies the file path and file name on the FTP server to be downloaded, and the $file_path parameter specifies the local file path to be saved after downloading. Similarly, the FTP_ASCII parameter is used to indicate file downloading in ASCII mode.

4. Close the connection

After completing the file transfer, in order to release resources and close the connection, you need to use the corresponding function.

<?php
// 连接FTP服务器(略)

// 文件传输(略)

// 关闭连接
ftp_close($ftp_conn);
?>

In the above code, we use the ftp_close() function to close the FTP connection.

Summary:

Through the above code examples, we can see that it is very simple to use PHP to implement FTP file transfer through the command line. Just use PHP's FTP extension function to connect to the FTP server, upload files, download files, and close the connection. By properly calling these functions, we can easily transfer files on the command line. Of course, according to actual needs, we can also expand these basic functions, add other operations, and implement more functions.

I hope this article will help you understand the methods of PHP and FTP file transfer, and you can quickly transfer files through the command line.

The above is the detailed content of PHP and FTP: How to transfer files through the command line. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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