Working with FTP by means of PHP

Working with FTP by means of PHP

FTP protocol (File Transfer Protocol) is one of the oldest Internet protocols used for the file transferring between two hosts.

The following actions are carried out on working with FTP:

  • Connection to the remote FTP-server
  • Registration on the FTP-server
  • Files downloading and uploading
  • Connection closing
  • Connection with the FTP-server
  • Connection with the FTP-server is carried out y means of ftp_connect function:

Syntax:

ftp_connect (string host [, int port [, int timeout]])

The required parameter of that function is the host name host. Second optional parameter port indicates the port number. If that parameter isn’t defined connection is carried out via the standard 21 port. Third optional parameter defines the time of the script running; its default value is 90 seconds.

Connection to the remote FTP-server

<?php
 $host 
"ftp://ftp.server.com";
 
$connect ftp_connect($host);
 if(!
$connect)
 {
 echo(
"Connect failed");
 exit;
 }
 else
 {
 echo(
"Connected"); 
 }
?>

Registration on the FTP-server

After the successful connection you have to register on the server. You can do that using ftp_login function:


<?php
 $user 
"maks";
 
$password "password";
 
$result ftp_login($connect$user$password);
?>

According to the listing ftp_login function has three parameters: FTP connection descriptor returned by ftp_connect function, username and password.

Notice. Registration isn’t required if the FTP server is anonymous.

Connection closing

After the communication session with the FTP server you have to close the connection by means of the ftp_quit function. The only parameter is the FTP connection descriptor:

Syntax:

ftp_quit($connect);

Files’ downloading from the server.

Files’ downloading from the server is carried out by means of the ftp_get function:

Syntax:

bool ftp_get(int ftp_connect, string local_file,  string remote_file, int mode);

mode argument should be indicated as a FTP_BINARY or FTP_ASCII constant. FTP_ASCII mode is used only for transferring of the files which consist of the ASCII symbols (i.e. text files); binary mode is used for the rest files transferring.

For example:

<?php
 $local_file 
"/users/local.txt";
 
$remote_file "remote.txt";
 
ftp_get($connect$local_file$remote_fileFTP_BINARY);
?>

Passing to the parent directory

Changing the current working directory to the parent one is carried out by means of the ftp_cdup function:

Syntax:

bool ftp_cdup(int ftp_connect);

Example:

<?php
 $host 
"ftp.server.com";
 
$port 21;
 
$user "anonymous";
 
$passwrod "password";
 
$connect ftp_connect($host$port150);
 if(!
$connect)
 {
 exit(); 
 }
 
$result ftp_login($connect$user$password);
 if(
$result)
 {
 
// save the current working directory name
 
$current_dir ftp_pwd($connect);
 
// pass to the parent directory 
 
ftp_cdup($connect);
 
// save the new directory name
 
$new_dir ftp_pwd($connect);
 }
 else
 {
 
ftp_quit($connect);
 exit();
 }
 
// close the connection
 
ftp_quit($connect);
?>

Changing the current working directory

Changing the current working directory to the indicated one is carried out by means of the ftp_chdir function:

<?php
 $new_dir 
"web";
 
ftp_chdir($connect$new_dir);
?>

According to that listing ftp_chdir function gas two parameters: connection descriptor $connect and the name of the new directory $new_dir.

Files’ deleting

Files can be deleted from the FTP server by means of the ftp_delete function:

Syntax:

bool ftp_delete(int ftp_connect, string remote_file);

ftp_connect is a connection descriptor, remote_file is the name of the deleted file.

Creating of the directory

New directory can be created by means of the ftp_mkdir function:

Syntax:

string ftp_mkdir(int ftp_connect, string directory);

That function returns created directory name or false in the case of the failure:

<?php
 $dir 
"web";
 
$created_dir ftp_mkdir($connect$dir);
?>

Files’ enumeration in the directory

You can learn the files in the current directory by means of the ftp_nlist function:

Syntax:

array ftp_nlist(int ftp_connect, string directory);

Example:

<?php
 $file_list 
ftp_nlist($connect".");
 if(
is_array($file_list))
 {
 foreach(
$file_list as $file)
 {
 echo(
"$file <br>");
 }
 }
?>

Files’ rename

Files can be renamed by means of the ftp_rename function:

Syntax:

bool ftp_rename(int ftp_connect, string from, string to);

Directory deleting

To delete the directory use ftp_rmdir function:

Syntax:

bool ftp_rmdir(int ftp_connect, string directory);

Defining the file size

For defining the file size you can use ftp_size function:

Syntax:

int ftp_size(int ftp_connect, string filepath);

In the case of the failure function returns -1. For example:

<?php
 $file 
"/web/user/file.txt";
 
$file_size ftp_size($connect$file);
 if (
$file_size == -1)
 {
 echo(
"Files size isn’t defined");
 }
 else
 {
 echo(
"File $file size is $file_size bytes");
 }
?>

Sending the command to the server

Command can be sent to the server by means of the ftp_site command:

Syntax:

bool ftp_site(int ftp_connect string command);

That function sends to the server SITE command. As a rule by means of the SITE command there executed such commands as chmod. You can learn the list of the SITE command having connected to the server and run the REMOTEHELP command. The code that allows to change the file access rights is shown below.

<?php
 $command 
"chmod 0766 /web/user/file.txt";
 
ftp_site($connect$command);
?>

 
  • Top