Downloading or Uploading files to the server via FTP is an essential task for every web developer should know. There are many FTP clients available for handling files on the FTP server. You can also connect FTP server using a PHP script and handle file operations.
PHP provides various functions to handle FTP server operations and there are many other libraries for the same. In this tutorial, we will show you how to connect FTP server in PHP and we will also take some examples to download and upload files to the FTP server.
FTP Server Connection in PHP
FTP works just like databases. This means before performing any operations we need to connect it using credentials and server address. The ftp_connect() function is used to connect PHP and FTP servers.
Once we establish a connection with the FTP server then we can perform a login attempt using the ftp_open() function by providing a valid username and password. Let’s take an example of connecting and logging FTP server using PHP.
<?php
// FTP server details
$ftp_server = 'ftp.example.com';
$username = 'USERNAME';
$password = 'PASSWORD';
$ftp = ftp_connect($ftp_server)
or die("Could not connect to $ftp_server");
if($ftp){
echo "Connected to the ftp server!";
$login = ftp_login($ftp, $username, $password);
if($login) {
echo "<br>Logged in successfully!";
}
else {
echo "<br>Login failed!";
}
// Closing the connection
ftp_close($ftp);
}
?>
Here, first of all, we have defined the server and credentials into variables then try to connect with the FTP server using the ftp_connect() function. If it connects to FTP then it will try to log in using our credentials and print a message as a response. It will print output something like below.
Connected to the ftp server!
Logged in successfully!
While connecting we can also specify a port like the below syntax :
$ftp = ftp_connect($ftp_server, 21) //Your Port
After login, we can perform operations like upload, download, and delete. We will use the ftp_put() function to upload files on the server. The ftp_put() function requires a few parameters like FTP object, remote file name, local file name, and mode.
Both remote and local paths need to be full paths and the mode must be FTP_ASCII or FTP_BINARY. Let’s take an example of uploading files to an FTP server.
<?php
$file = 'somefile.txt';
$remote_file = 'readme.txt';
$ftp = ftp_connect($ftp_server);
$login = ftp_login("FTP_ADDRESS", "USERNAME", "PASSWORD");
if($login){
// upload a file
if(ftp_put($ftp, $remote_file, $file, FTP_ASCII)) {
echo "File Uploaded\n";
} else {
echo "Something went wrong while uploading file.\n";
}
}else{
echo "<br>Login failed!";
}
// close the connection
ftp_close($ftp);
?>
In the above example, first of all, we will connect and try to log in using the given credentials. Then it will try to upload a file using the ftp_put() function and print the message as per response.
There is some other functions like ftp_pasv() or ftp_fput() which can be used to upload files to the FTP server.
Download File from the FTP Server Using PHP
To download files, we can use the ftp_get() function. It requires some parameters as an upload function but in a different order. Let’s understand more using examples.
<?php
$local_file = 'somefile.txt';
$remote_file = 'readme.txt';
$ftp = ftp_connect($ftp_server);
$login = ftp_login("FTP_ADDRESS", "USERNAME", "PASSWORD");
if($login){
// upload a file
if(ftp_put($ftp, $local_file, $remote_file, FTP_ASCII)) {
echo "File Downloaded\n";
} else {
echo "Something went wrong while downloading file.\n";
}
}else{
echo "<br>Login failed!";
}
// close the connection
ftp_close($ftp);
?>
Delete File From FTP server
To delete the a file, you can use the ftp_delete() function. It just requires two parameters the first one is the FTP connection object and the second one is the file name that you want to delete.
<?php
$ftp = ftp_connect($ftp_server);
$login = ftp_login("FTP_ADDRESS", "USERNAME", "PASSWORD");
if($login){
// upload a file
if(ftp_delete($ftp, 'remove.txt')) {
echo "File deleted\n";
} else {
echo "Something went wrong.\n";
}
}else{
echo "<br>Login failed!";
}
// close the connection
ftp_close($ftp);
?>
Conclusion
In this article, we have learned about FTP connection, logging in to the FTP server, and performing operations like uploading, downloading, and deleting files using PHP. But there are many other FTP operations and functions that we can use as per our requirements like executing some commands, getting file size, or changing file permissions.