Connect To FTP Server Using PHP

FTP connection functions have been built into PHP since version 4 and make transferring files through FTP very easy.

The main function involved is called ftp_connect() which takes a FTP host as a parameter and attempts to connect to it. The port and a timeout limit can also be added to the function if needed.

Once a connection has been made then the ftp_login() function is used to attempt a login. This function returns true on success and false if it fails. The following snippet of code will attempt to connect and login to an FTP server, if any step fails then the code will print out a message saying so.

$host= 'ftp.example.com';
$user = 'notarealusername';
$password = 'notarealpassword';
$ftpConn = ftp_connect($host);
$login = ftp_login($ftpConn,$user,$password);
// check connection
if ((!$ftpConn) || (!$login)) {
 echo 'FTP connection has failed! Attempted to connect to '. $host. ' for user '.$user.'.';
} else{
 echo 'FTP connection was a success.';
 $directory = ftp_nlist($ftpConn,'');
 echo '<pre>'.print_r($directory, true).'</pre>';
}
ftp_close($ftpConn);

The ftp_close() function takes the resource identifier and a closes it. Here is what is printed out if the code fails.

FTP connection has failed! Attempted to connect to ftp.example.com for user notarealusername.

If the connection is a success then the script attempts to retrieve the contents of the root directory, this is done using the ftp_nlist() function. Here is a typical example of what might be found if the code successfully connects to an FTP server.

FTP connection was a success.
Array
(
 [0] => cgi-bin
 [1] => logfiles
 [2] => html
)

There are a lot of different FTP functions available, covering the main things that would expect any FTP program to do. The main ones you might use are ftp_get() to download files, ftp_put() to upload files and ftp_nlist() to view the contents of a directory. There is also a function called ftp_chmod() which allows you to set the permissions of a directory on your FTP server.

Comments

Hi, Thanks for this helpful code.

I would like to ask about the code in viewing the files in that root directory.

Thanks,

Permalink
Warning: ftp_login() [function.ftp-login]: Login authentication failed in /home/demorct/public_html/nitish/ftp/test.php on line 13 i write the code exactly you given but still this warning come
Permalink
I think the clue to this error is in the phrase "Login authentication failed". Perhaps your FTP server has a different username and password?
Name
Philip Norton
Permalink
what is that $directory?plz tell me
Permalink
The $directory variable will be an array of filenames from the specified directory on success or FALSE on error.
Name
Philip Norton
Permalink
Warning: ftp_connect() [function.ftp-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. Warning: ftp_login() expects parameter 1 to be resource, FTP connection has failed! Attempted to connect to ftp.example.com for user XXXXXXXXX . i got this type of warning in your script. can you give me suggestion to solve this problem. thanks in advance.
Permalink
Sounds like your FTP address isn't valid.
Name
Philip Norton
Permalink
hello, I am getting success from ftp_connect and login function but I am not able to change directory or upload any file on ftp. It gives me an error as ftp_chdir(): Failed to change directory and ftp_put(): Could not create file respectively. Please help me on this as soon as possible.
Permalink

Great tutorial, thanks, there is also another great  FTP PHP script, that you can use to upload and manage files on server with any hosting account from Hostgator or Bluehost or PlotHost, this PHP Script is available on Github and SorceForge by searching for SabroCMS.

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
1 + 8 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.