Php ftp learning (1)

Source: Internet
Author: User
Tags ftp connection
ByVikramVaswaniMelonfireNovember07, 2000 we are a loyal FANS of a group of PHP. We use it for various reasons-WEB site development, drawing, database connection, etc.-we found that, it is very friendly, powerful, and easy to use ...... You may have seen how PHP is used to create GIF and JPEG images and dynamically retrieve messages from the database By Vikram Vaswani
Melonfire
November 07,200 0
We are a loyal FANS of a group of PHP, and we use it for various reasons-WEB site development, drawing, database connection, etc.-we found it very friendly, powerful and easy to use ......
You may have seen how PHP is used to create GIF and JPEG images and dynamically retrieve information from the database, but this is only the tip of the iceberg-the latest version of PHP has a powerful file transfer function.
In this tutorial, I will show you how FTP transfers files through HTTP and FTP connections, as well as some simple program code. come with me!

First, you should know that PHP transfers files through HTTP and FTP connections. Uploading files via HTTP has already appeared in PHP3, and now the new FTP function has appeared in the new PHP version!
Before you start, make sure that your PHP supports FTP. you can check the following code:

--------------------------------------------------------------------------------

Phpinfo ();

?>
--------------------------------------------------------------------------------
Check the output result. there is an "Additional Modules" area, which lists the Modules supported by your PHP. if you do not find the FTP module, you 'd better reinstall PHP and add FTP support!

Let's take a look at how a typical FTP task is completed!
--------------------------------------------------------------------------------
$ Ftp ftp.server.com
Connected to ftp.server.com
220 server.com FTP server ready.
Name (server: john): john
331 Password required for john.
Password:
230 User john logged in.
Remote system type is UNIX.
Using binary mode to transfer files.
Ftp> ls
200 PORT command successful.
150 Opening ASCII mode data connection for/bin/ls.
Drwxr-xr-x 5 john users 3072 Nov 2.
Drwxr-xr-x 88 root 2048 Nov 1 ..
Drwxr -- r -- 2 john users 1024 Oct 5 bin
Drwx -- x 8 john users 1024 Nov 2 public_html
Drwxr -- r -- 4 john users 1024 Nov 2 tmp
-Rw-r -- 1 john users 2941465 Oct 9 data.zip
226 Transfer complete.
Ftp> bin
200 Type set to I.
Ftp> get data.zip
Local: data.zip remote: data.zip
200 PORT command successful.
150 Opening BINARY mode data connection for data.zip (2941465 bytes ).
226 Transfer complete.
Ftp> bye
221 Goodbye.
--------------------------------------------------------------------------------
As you can see, the process is clearly divided into several segments: Connection (establish a connection with the FTP server), verification (determine whether the user has the right to enter the system), transmission (including the column Directory, upload or download files), cancel the connection.

Procedure of using PHP for FTP
Follow these steps to create an FTP connection for PHP: open a connection-send authentication information-use PHP functions to manipulate directories and transfer files.
Specific implementation:
--------------------------------------------------------------------------------

// Connect to the FTP server
$ Conn = ftp_connect ("ftp.server.com ");

// Use username and password to log on
Ftp_login ($ conn, "john", "doe ");

// Obtain the remote system type
Ftp_cmdype ($ conn );

// List objects
$ Filelist = ftp_nlist ($ conn ,".");

// Download an object
Ftp_get ($ conn, "data.zip", "data.zip", FTP_BINARY );

// Close the connection
Ftp_quit ($ conn );

?>
--------------------------------------------------------------------------------
Let's step by step:
To complete an FTP connection, PHP provides the ftp_connect () function, which uses the host name and port as the parameter. In the preceding example, the host name is "ftp.server.com". if the port is not specified, PHP uses "21" as the default port to establish a connection.
After the connection is successful, ftp_connect () returns a handle. this handle will be used by subsequent FTP functions.
--------------------------------------------------------------------------------

// Connect to FTP server
$ Conn = ftp_connect ("ftp.server.com ");

?>
--------------------------------------------------------------------------------
Once a connection is established, use ftp_login () to send a user name and password. As you can see, this function ftp_login () uses handle from the ftp_connect () function to confirm that the user name and password can be submitted to the correct server.
--------------------------------------------------------------------------------

// Log in with username and password
Ftp_login ($ conn, "john", "doe ");

?>
--------------------------------------------------------------------------------
At this time, you can do what you want to do, specifically in the next section:

After you finish what you want, remember to use the ftp_quit () function to close your FTP connection.

--------------------------------------------------------------------------------

// Close connection
Ftp_quit ($ conn );

?>

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.