Connecting to and Using an FTP with PHP



PHP


In this article, we show how to connect to and use a file transfer protocol (FTP) with PHP.

Being a server side language, PHP can connect to remote servers such as FTP servers to allow for transferring of files.

PHP has an ftp_connect function which allows for connection to an FTP server.

In order for the connection to be successful, the address that you put in the parameter of the ftp_connect() function has to be an FTP server.

You can't just put in your website hosting name or IP address and expect it to work. This PHP function only works if the address is an FTP server.

That's the first thing.

The next PHP function ftp_login allows you to log into the FTP server (assuming it was able to connect to the server). You need a username and password to get access into the FTP server.

Otherwise, you won't be able to transfer files or do anything.

So, again, you need to connect to an actual FTP server and you connect to it via the ftp_connect() function. Next, you need a username and password to gain access into the FTP server and this is done via the PHP ftp_login function.

We show how to do these in the code below.

PHP Code

The PHP code to connect to an FTP server and log into the FTP server is shown below.



So now we'll go over the code.

So, first, we create a variable named $connect and make it equal ftp_connect("ftp.godaddy.com") or die("Connection to server unsuccessful"); If the statement executes, this variable holds the value of true. If not, the statement, "Connnection to server unsuccessful" is shown. If unsuccessful, the die() function terminates the script and what it has in it will print.

The ftp_connect function, besides, having the hosting name of the website as its parameter can store the IP address of the website instead. So, for example, the line can be, ftp_connect("

If it has been successful, the script will continue on.

We have 2 variables, $username and $password. They store the username and password to log in to the FTP server.

We then create a variable $login that is set equal to ftp_login() function which has 3 parameters. $connect is the past parameter which gave connected to the server. $user is the username. $password is the password.

If nothing prints in this script and you get a blank page, you have successfully logged in.

If the login is unsuccessful, the die() function is called and the statement, "Login was unsuccessful" is output.

So this is all that is required to connect to and log into an FTP server.

PHP then has a number of function so that we can interact and do useful things with the FTP servers such as create, delete, rename, copy, get a listing of, and transfer files, as an FTP does.


Getting a Directory Listing

One way to get a directory listing with PHP is through the ftp_nlist() function.

The ftp_nlist() function gets a directory listing from the remote computer and stores it in an array.

The ftp_nlist() function takes 2 parameters. The first parameter establishes connection, via the $connection variable we previously used. And the second parameter is where we specify the name of the directory we want to get the listing of.

the full PHP code is shown below.



So this code prints all the files in the directory you specified.

Downloading Files with an FTP

Files can be downloaded with an FTP from the remote computer with the ftp_get() function.

The following code can be used below to download a file from the computer once you've logged into the FTP server.



So the ftp_get() function takes 4 parameters.

The first parameter uses the previously used $connection variable to establish connection.

The second parameter, "file_name.txt", is the name of the file that you will have on your computer once the file is downloaded. This is what you want to name the file.

the third parameter, "file_downloaded.txt", is the name of the file on the server that you want to download.

The FTP_ASCII parameter tells the FTP what kind of file is being downloaded. It can either be text files (FTP_ASCII) or machine language files (FTP_BINARY), which is basically anything that isn't plain text. You can determine which one you need by the contents of the file.If the contents are regular characters that are human readable, that you can read and understand, it is an ASCII file. IF the contents are all machine code, ones and zeros, the file is binary. Graphic files, for example, are binary.


Uploading Files with an FTP

Files can be uploaded with an FTP to the remote computer with the ftp_put() function.

The following code can be used below to upload a file to the computer once you've logged into the FTP server.



So the ftp_put() function takes 4 parameters.

The first parameter uses the previously used $connection variable to establish connection.

The second parameter, "file_name.txt", is the name of the file that you have on your computer that you want to upload.

the third parameter, "file_downloaded.txt", is the name that you want the file to be once it is uploaded to the server.

The FTP_ASCII parameter, again, specifies the file is an ASCII file.

Once you've finished transferring files over your FTP connection, you can close the connection with the following statement shown belo.



This closes the FTP connection.


Below is a table of more PHP FTP functions that are commonly used.

Function What It Does
ftp_cdup($connection) Changes to the directory directly above the current directory
ftp_chdir($connection, "directoryname") Changes directories on the remote computer
ftp_delete($connection, "path/filename") Deletes a file on the remote computer
ftp_exec($connection, "command") Executes a system command on the remote computer.
ftp_mdtm($connection, "filename.txt") Gets hte time when the file was last modified.
ftp_mkdir($connection, "directoryname") Creates a new directory on the remote computer.
ftp_pwd($connection) Gets the name of the current directory on the remote computer.
ftp_rename($connection, "oldname", "newname") Reames a file on the remote computer
ftp_rmdir($connection, "directoryname") Deletes a directory on the remote computer.
ftp_size($connection, "filename.txt") Returns the size of the file on the remote computer.
ftp_systype($connection) Returns the system type of the remote file server (for example, Unix)



Related Resources

How to Create a New Directory Using PHP

How to Upload Files to Your Server Using Your Own Website

How to Restrict the Size of a File Upload with PHP

How to Create Your Own File Transfer Protocol (FTP) For Your Website Using PHP

How to Create a Register and Login Page Using PHP

How to List All Files in a Directory using PHP

How to List All the Directories of a Website Using PHP

How to List All Files of the Home Directory and Its Subdirectories of a Website Using PHP

How to Read the Contents of a File Using PHP

How to Write Contents to a File Using PHP

How to Copy a File Using PHP

How to Rename a File Using PHP

How to Delete a File Using PHP



HTML Comment Box is loading comments...