How connect and handle files in FTP server using PHP?

| | 4 min read

There will be many situations when you need to handle files from an FTP server in PHP. PHP provides a handful of functions to do the task quite easily, but there are some hidden difficulties too. So here I am going to explain the process of connecting to and FTP server and handling files there.

Connecting and logging in

First you need to connect and login to the ftp server before any further operations can be done. To connect to an FTP server you can use the function ftp_connect. This function can be used to connect to an ftp server with a specified port and a timeout(both are optional). On success this function will return an FTP stream and false on failure.

resource ftp_connect ( string $host [, int $port = 21 [, int $timeout = 90 ]] );

After making a successful connection you need to login using the credentials you have in your hand. You can use the function ftp_login for this purpose.

bool ftp_login ( resource $ftp_stream , string $username , string $password )

The $ftp_stream here is the resource returned from the ftp_connect function. Username and password are the login credentials for the ftp server. This will return a TRUE for success and FALSE for an unsuccessful connection.

Setting the connection to passive mode.

For doing most of the operations you need to make the ftp connection stream to passive mode. The function ftp_pasv can be used for this purpose. Since in passive mode the data connections are initiated by the client rather than the server, This will be mostly required if the server is behind a firewall.

bool ftp_pasv ( resource $ftp_stream , bool $pasv );

The ftp_stream is the ftp connection object and the second parameter can be either true or false, which indicates the passive mode status.

If this is not set then most of the operations couldn't be carried out. Also connection will lose continuously and there may be other problems that needed to be handled. To avoid all these, it is better to open the passive mode with the ftp server.

Listing and retrieving files.

Usually, you need to list files in a particular directory in the ftp server, and then have to go through them to process them. You can use the function ftp_nlist. This function will return a list of files in the given directory.

array ftp_nlist ( resource $ftp_stream , string $directory );

This function will return an array of files from the directory given. You can use '.' to indicate the current directory. If we haven't set the passive mode in the last step, then may be this function will always return false, even if there are files to be listed in the directory given. You can also use the function ftp_rawlist, which will returns a more detailed list of files in the given directory.

mixed ftp_rawlist ( resource $ftp_stream , string $directory [, bool $recursive = false ] );

If the third parameter is TRUE then it will list files recursively for all the directories inside the given directory.

Retrieve a file for local processing

After knowing the filename by listing them in last step(or you already know a filename), you have to download it for local editing. For this you can use the function ftp_fget. This function can be used to get the file specified and save it to a local one.

bool ftp_fget ( resource $ftp_stream , resource $handle , string $remote_file , int $mode [, int $resumepos = 0 ] )

Here the ftp_stream is the connection object, $handle is an open file pointer to which the file needed to be downloaded. remote file is the remote file path and mode is either FTP_ASCII or FTP_BINARY. The last parameter determines the position in the remote file to start downloading from which is optional.

In some cases you don't want to download the file to a local files, instead you need to process the files on the fly, you can do it by creating a temporary file pointer opened. It can be done as,

$tempHandle = fopen('php://temp', 'r+');
  if (@ftp_fget($conn_id, $tempHandle, $filename, FTP_ASCII, 0)) {
    rewind($tempHandle);
    return stream_get_contents($tempHandle);
  }

Here the $tempHandle is opened using the PHP's read or write streams which can be used to open a file in a temporary file, the file size limit is default to 2 MB. You can also use php://memory which will store the file contents in the memory itself.

Upload a file

To upload a file to the ftp server you can use the function ftp_fput. This function can upload from an open file handler to a remote location.

bool ftp_fput ( resource $ftp_stream , string $remote_file , resource $handle , int $mode [, int $startpos = 0 ] )

Here $remote_file is the remote path and filename to save the file, $handle is the local opened file. This function will return a true for a successful upload and false if unsuccessful.

Delete a file

At some point of time you may need to delete some files from the ftp server, in that cases you can use the function ftp_delete.

bool ftp_delete ( resource $ftp_stream , string $path );

The $path is the path of the file that needed to be deleted. Will return a TRUE for a successful deletion and FALSE if not. There are many other functions that helps us to handle files in the FTP server in an easy manner. Just find and try them. Happy Coding.

For any business help, get a quote now or contact us.