by Vikram Vaswani
Melonfire
November 07, 2000
We are a group of PHP fans, we use it for a variety of reasons-web site development, drawing, database connectivity, etc.-we found that it is very friendly, powerful and easy to use ...
You may have seen how PHP was used to create GIF and JPEG images, get information dynamically from the database, and so on, but this is just the tip of the iceberg---the latest version of PHP has a powerful file transfer capability.
In this tutorial, I'll show you how FTP transmits files via HTTP and FTP connections, and there are some simple program codes to follow.
First, you should know that PHP transmits files through HTTP and FTP joins. Uploading files over HTTP has already appeared in PHP3, and now the new FTP function has appeared in the new PHP version!
Before you start, you need to be sure that your PHP supports FTP, and you can use the following code to find out:
?>
--------------------------------------------------------------------------------
Check the output, there is a "Additional Modules" area, here is a list of your PHP-supported modules; If you don't 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 done!
--------------------------------------------------------------------------------
$ ftp ftp.server.com
Connected to Ftp.server.com
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
PORT command successful.
Opening ASCII mode data connection For/bin/ls.
Drwxr-xr-x 5 John users 3072 Nov 2 11:03.
Drwxr-xr-x the root root 2048 Nov 1 23:26.
drwxr--r--2 John users 1024 Oct 5 13:26 Bin
Drwx--x--x 8 John users 1024 Nov 2 10:59 public_html
drwxr--r--4 John users 1024 Nov 2 11:26 tmp
-rw-r--r--1 John users 2941465 Oct 9 17:21 data.zip
Transfer complete.
Ftp> bin
Type set to I.
Ftp> Get Data.zip
Local:data.zip Remote:data.zip
PORT command successful.
Opening BINARY mode data connection for Data.zip (2941465 bytes).
Transfer complete.
Ftp> bye
221 Goodbye.
--------------------------------------------------------------------------------
As you can see, the process is clearly divided into several segments: joins (a connection to the FTP server), authentication (determining whether the user has the power to enter the system), transmission (this includes the column directory, uploading or downloading files), and canceling the join.
To use PHP for FTP
Creating a PHP FTP join must follow the basic steps of opening a join-issuing authentication information-manipulating directories and transferring files using PHP functions.
The following specific implementation:
--------------------------------------------------------------------------------
?
Join an FTP server
$conn = Ftp_connect ("ftp.server.com");
Log on using username and password
Ftp_login ($conn, "John", "Doe");
?>
--------------------------------------------------------------------------------
Let's take this step-by-step:
To initialize an FTP join, PHP provides the Ftp_connect () function, which uses the host name and port as parameters. In the example above, the host name is "Ftp.server.com", and if the port is not specified, PHP will use "21" as the default port to establish the join.
Ftp_connect () returns a handle handle after the join succeeds, and this handle will be used by the FTP function used later.
--------------------------------------------------------------------------------
?
Connect to FTP Server
$conn = Ftp_connect ("ftp.server.com");
?>
--------------------------------------------------------------------------------
Once a join is established, use Ftp_login () to send a user name and user password. As you can see, this function Ftp_login () uses the handle from the Ftp_connect () function to determine that the user name and password can be committed to the correct server.
--------------------------------------------------------------------------------
?
Log in with username and password
Ftp_login ($conn, "John", "Doe");
?>
--------------------------------------------------------------------------------
At this point, you can do what you want to do, specifically in the next section:
After doing what you want to do, remember to use the Ftp_quit () function to turn off your FTP connection
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.