The FTP protocol has been established in the aim of facilitating exchange of data between computers. FTP allows you to tranfer data between two servers, or servers and clients, and nearly all servers support this protocol. FTP tools are most often used by webmasters to transfer files on the server, and net surfers to download them. Commands of directory management may help to update a website
FTP and HTTP have same model
HTTP (HyperText Transfer Protocol) is the data transfer protocol on the
Web (World Wide Web) while FTP (File Transfer Protocol) is a protocol for
exchanging data between computers.
HTTP works also in the client/server mode, but the client is called "user agent" and is a browser or other tool to access the Web, such as Chromeless which can put web applications as icons on the computer desktop.
The connections are made both in HTTP and FTP as a request in TCP (Transmission Control Protocol). HTTP uses methods (GET, POST, HEAD) while FTP makes use of commands, and the first has its own error codes, as the code 404 (access unavailable) that users are familiar with...
Description of the access to the server
FTP provides a set of commands needed for the transfer and the management
- Download and upload.
- View the contents of directories
- Rename or delete files, change permissions, etc.
FTP is based on the TCP/IP protocol, which is also the one of Internet or
Intranet. The FTP protocol is operating system independent, on servers and
There is a connection established for commands and another for data transfer.
- The connections are made in both directions, between two computers that are both servers or clients. But we can connect as a client only.
- The data connection is not permanent. Sending data is simultaneously in both directions.
Common operations begin with a client's connection, your computer for example, on the server, which usually asks you a login and a password. But anonymous connection is also possible. This mode is used for example by Sourceforge for sending files that are stored on a storage area, before to be be checked and integrated into the site
The connection can operate in active or passive modes. In the active mode,
the server opens a connection on the client to send data (even if it is the
client who pass commands), while in passive mode, it is the client computer
that initiates the transfer.
The active mode could face to firewalls, intended to protect the computer against malicious external intrusions.
FTP and PHP
The PHP language offers a comprehensive enough list of functions to manage
the contents of a website by FTP, making it possible to realize the software
on this site.
In addition to the commands of connection and disconnection, the main functions are:
- ftp_get to download a file.
- ftp_put to send it.
- ftp_chdir, ftp_mkdir can change directories, or create one.
- ftp_size to get the size of a file,.
Some functions or parameters depend on compatibility with the server, and that makes the achievement of universal software rather difficult.
FTP with PHP, démo
The ftp access starts with a connection to the Web site on the server, according to its address of the form: ftp.domainename.tld.
int x = ftp_connect ("ftp.scriptol.com")
This function requires a small delay, and returns a number which will be
used for all further ftp functions.
Thus, to finish the session one will do:
Once connected, one send a login and a password:
boolean result = ftp_login (x, "login", "password")
If the user is recognized, the function returns true and one can now execute
the desired operations.
For sending a file to the server:
ftp_put (x, remotepath, localname, mode)
For downloading a file from the server:
ftp_get (x, remotepath, localname, mode)
The mode is FTP_ASCII for a textual file and FTP_BINARY for a binary file, an image for example.
The archive of the source code is at bottom.
Binary and ASCII modes
As this can be seen when using PHP functions, the transfer method can be
made either textual in ASCII, or binary.
In the first case, the files are saved in the format of the target operating system. Text file in Windows' format sent from a Windows PC will be stored in Unix format if the OS of the remote computer is Unix. Instead in binary mode files are stored as is, what is appropriate for images.
If you are using a FTP program, it will not be able to adapt the transfer mode to each file, and thus it uses only the binary mode for all files, therefore the format of text files will not be converted. This is crucial for the .htaccess file. Use an editor that allows to choose the OS format.
Security requires an reinforced protocol
FTP is not secure because the password is not encrypted and can be intercepted
by spyware. As a general rule, transfers are not encrypted and can be intercepted.
To avoid this, the SFTP protocol (SSH File Transfer Protocol), has been developed. It differs from FTP and requires its own tools.
The other one is secure FTPS (FTP over SSL), which adds an SSL encryption.
- FTP in PHP. Demo of upload and download.
- PHP FTP Synchronizer. Updating a Web site from a local directory.
- FTP Check. Testing connection to a server.
- FTP Download. Complete program to download a file.
- The rfc959 standard. Specification of the FTP protocol.
- Comparison between FTP and SFTP. Characteristics of the two protocols, and also SCP.
- The active and passive mode. Very technical comparison.
- The list of FTP commands. With also a link on the commands of the FTP program on Windows.