Introduction
FTP is abbreviation for ‘File Transfer Protocol’. The FTP functions in PHP allows an user to connect to and login into a server, creating and removing directories, listing files in the current directory, execute a command, upload and download files to and from the server. It also allows to get and set some runtime options, set a no-blocking option, get a file size, sends a SITE command to the server etc. For this, PHP has standardised set of functions. The FTP functions in PHP were first incorporated in PHP3, later versions have added more to it.
Working with Built in FYP functions in PHP
PHP provides over 34 functions and 10 FTP constants, which together allows you to build robust stand alone PHP based FTP system. In this article, we will consider some of the most used function and see how to use them.
Connecting to a FTP server
The ftp_connect() function opens an FTP connection to a server. While is connection is open you can run other FTP functions.
When the connection is open, you can run FTP functions against the server.
Syntax of this function is: ftp_connect(host, port, timeout)
Where, host is the server name or IP address, port is the port number of connection, by default ftp works in port 21 and timeout is optional it indicates the timeout for the connection with default value 90
<?php
$conn = ftp_connect("ftp.testdomain.com") or die("Could not connect");
?>
Login to an FTP server
After you have connected to a machine, next job will naturally be to log into it. The ftp_login() function allows an user to log in into a server.
Syntax of this function is: ftp_login(ftp_conn,username,password)
Where, ftp_conn is the connection established, username and password is the login username and password.This function returns TRUE on success and FALSE and a warning on failure.
<?php
$conn = ftp_connect("ftp.testdomain.com") or die("Could not connect");
ftp_login($conn,"admin","pass123");
ftp_close($conn);
?>
Retrieving files
You are going to retrieve a file and might not know the filename exactly. Before, retrieving the file you can do some sleuthing. First, let’s retrieve a list of files from a specific directory.
$list = ftp_rawlist($resource, ‘/mydir’);
This will return a raw list of all the files in a directory. Depending on the operating system, a series of information could be returned for each file name. The ftp_systype() function will even show you the OS of the FTP server.
PHP provides the following two file retrieving functions:
- ftp_fget() Downloads a file from the FTP server and saves it to an open file.
- ftp_get() Downloads a file from the FTP server.
Both the functions take same list of parameters
The syntax :
ftp_fget(ftp_conn,remote,local,mode,resume)
where, remote is a required parameter which specifies the path of the file to copy, local is also a required parameter specifies the path of the open file to paste into and mode is also required parameter specifies the mode of write. It has two values: FTP_ASCII and FTP_BINARY. resume is an optional parameter it specifies the place in the file to start copying, default value is 0 for beginning.
<?php
$source = "source.txt";
$target = fopen("target.txt", "w");
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","pass123");
ftp_fget($conn,$source,$target,FTP_ASCII);
ftp_close($conn);
?>
ftp_get has same syntax: ftp_get(ftp_connection,local,remote,mode,resume)
<?php
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","pass123");
echo ftp_get($conn,"target.txt","source.txt",FTP_ASCII);
ftp_close($conn);
?>
Uploading files
PHP provides the following two file uploading functions:
- ftp_fput() uploads from an open file and saves it to a file on the FTP server.
- ftp_put()uploads from a local file to the FTP server.
Here again both the functions take same list of parameters.
The syntax: ftp_fput(ftp_conn,remote,local,mode,resume)
The following code shows the use:
<?php
$source = fopen("source.txt","r");
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","pass123");
echo ftp_fput($conn,"target.txt",$source,FTP_ASCII);
ftp_close($conn);
?>
For uploading a local file the ftp_put() function is used:
<?php
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","pass123");
echo ftp_put($conn,"target.txt","source.txt",FTP_ASCII);
ftp_close($conn);
?>
So now you know the basic ftp functions. You must not forget to close the connection once your job is done.