Categories
PHP

Connecting with FTP Server

PHP comes with built-in support for FTP and a special set of functions that implement the complete FTP functionality defined in the associated Request for Comment (RFC).

When accessing FTP servers, PHP’s stream wrappers come in very handy, as well. You only have read access, but the access is binary-safe nonetheless. This code shows how to download a file from an FTP server and save it to the local hard disk using file_get_contents().

Example: Reading files from FTP with file_get_contents()

<?php
 $file = file_get_contents('ftp://example.com/file.txt');
 echo $file;

If you do not provide any credentials, PHP tries to log you into the FTP server using a guest account most public FTP servers offer. You can, however, also provide the credentials yourself:

<?php
 $file = file_get_contents('ftp://USER:PASSWORD@example.com/file.txt');
 echo $file; // Prints file.txt content

Example: Writing files to FTP with file_put_contents():

You can write files to the FTP server with file_put_contents().

<?php
 $file = file_get_contents('/usr/brainbell/file.txt');
 file_put_contents('ftp://name:password@example.com/file.txt', $file);

You can also use the fopen() with the correct file mode for reading from FTP and writing to FTP. Though, both reading and writing simultaneously are not yet supported.

Using FTP Built-in Functions

PHP also comes with built-in support for FTP and a special set of functions that implement the complete FTP functionality defined in the associated Request for Comment (RFC). In the Windows distributions, this is enabled by default, whereas on other systems, PHP has to be configured with the switch enable-ftp. Also, make sure the extension=ftp is uncommented in php.ini file. Then, using the FTP server usually consists of the following steps:

  1. Connect to the server using ftp_connect()
  2. Log in using ftp_login()
  3. Go to the target directory using ftp_chdir()
  4. Read a file ftp_get() from or write a file ftp_put() to the FTP server
  5. Close the connection using ftp_close()

Because reading probably is more common than writing, the following shows the former task being executed:

<?php
 $ftp = ftp_connect('ftp.myhost.com');
 $login = ftp_login($ftp, 'username', 'Password');

 if (! ($ftp && $login )) {
  die ('FTP server connection failed');
 }
 // Changes the current directory on the FTP server
 ftp_chdir($ftp, '/htdocs/brainbell/');

 /* Downloading text file
    Download $remote_file and save it to $local_file */
 if (ftp_get($ftp, 'local_file.txt', 'remote_file.txt', FTP_ASCII) ) {
  echo 'File downloaded.';
 }
 
 /* Downloading binary file
    Download server.jpg and save it to local.jpg */
 if (ftp_get($ftp, 'local.jpg', 'server.jpg', FTP_BINARY) ) {
  echo 'File downloaded.';
 }

 // Upload a text file
 if (ftp_put($ftp, 'server.txt', 'local_file.txt', FTP_ASCII)) {
  echo 'local_file.txt uploaded';
 }
 
 ftp_close($ftp);

The syntax of ftp_get(), after the FTP resource, you have to provide first the local filename, then the remote filename. The last parameter is the transfer mode: FTP_ASCII for text files and FTP_BINARY for all other data.

FTP Directory Listing

<?php
 $ftp = ftp_connect('ftp.ex.com');
 $login = ftp_login($ftp, 'username', 'Password');

 if (! ($ftp && $login )) {
  die ('FTP server connection failed');
 }

 // Changes the current directory on the FTP server
 ftp_chdir($ftp, '/htdocs/brainbell/');
 
 // Returns the current directory name
 $dir = ftp_pwd($ftp); //brainbell
 
 // Returns an array of files in the given directory
 $files = ftp_nlist($ftp, $dir);
 
 foreach ($files as $file)
  echo $file . '<br>';
  1. ftp_chdir() function changes the directory on the remote computer.
  2. ftp_pwd() function gets the name of the current directory on the remote computer.
  3. ftp_nlist() function gets a directory listing from the remote computer and stores it in an array.

For additional FTP functions, visit https://php.net/manual/ref.ftp.php.


Communicating with Servers: