Create a simple FTP class

David Carr

5 min read - 6th May, 2011

FTP is well known to all designers/developers to moves files from a local computer to a remote web server, What if you want to copy a file from one server to another? the recommended choice would be to use SSH or telnet but if you don't have access to these then you would download the files via FTP and then upload them to the new location.

There may be times where you just want to copy a single file to another server connection via FTP and download then uploading seems a little tedious, a better way is to copy the file directly from the current server to the new server. To do this I'm going to create a simple OOP FTP class that will do the heavy lifting for me and let me repeat the process as many times as required.

NOTE: If this does not work on your server it may be a firewall blocking the connection.

Here's how I would call the class and a basic usage example.

//extend time limit
set_time_limit(200);

//include class
require('ftp.class.php');

$ftp = new FTP('www.domain.co.uk','username','password');              //connect via FTP
$ftp->ftpMkDir('www.domain.co.uk/directory-name');                        //make folder
$ftp->ftpFolderPermission('www.domain.co.uk/directory-name','0777');      //change folder permissions
$ftp->ftpCopyFile('www.domain.co.uk/directory-name/file.php','file.php'); //copy file from old server to new server

$ftp->ftpClose();

First I extend the time limit to allow me to move bigger files without the server timing out. I've also called my FTP class. To use the class I need to instantiate it this is done by creating a variable passing the new keyword followed by the class name note the class name is case sensitive I also need to pass the FTP SERVER, FTP username and FTP Password when I instantiate the class as it connected to the FTP server automatically.

$ftp = new FTP('www.domain.co.uk','username','password');

The class has 4 available methods which allow you to:

  • Make a directory
  • Change directory permissions
  • Copy file from the current server to the new server
  • Close FTP connection

To use the methods they require parameters passing to them which are:

  • ftpMkDir needs the server path followed by the directory name you want to create
  • ftpFolderPermission needs the path to the file, folloed by the permissions level
  • ftpCopyFile needs the location where to copy the file to, followed by the location on the current server relative to the scripts location

ftpClose() does not require anything being password to it, once called the FTP connection is closed.

Now you've seen how to use the class it's time to see write the class, first I create all the properties of the class, as I don't want them to be accessed outside of the class I define them all as private properties.

class FTP {

    private $host;
    private $user;
    private $pass;
    private $conn;
    private $dirToCreate;
    private $folderChmod;
    private $permission;
    private $remoteFile;
    private $localFile;

Next I create the constructor, inside the method I add the parameters to the equivalent properties. Connect to the FTP server by passing the host to ftp_connect then login by passing the reference to the connection followed by the username and password. I've also included an echo that let's us know a connections has been made.

public function __construct($host,$user,$pass){
        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;

        $this->conn = ftp_connect($this->host) or die("Could not connect to $host");
        ftp_login($this->conn,$this->user,$this->pass);
        echo "connected ok ".$this->host;
    }

The next method closed the connection when called, it's very simply call ftp_close and pass the reference to the server connection.

function ftpClose()    {
        ftp_close($this->conn);
    }

Next I create a method to create directories. the desired directory name is passed during the method call, which is passed to a property then passed to the function ftp_mkdir along with the connection reference, I've included a check to see if the directory exists if it does then method simply returns otherwise a confirmation message is echo'd.

function ftpMkDir($dirToCreate){

         $this->dirToCreate = $dirToCreate;
         @ftp_mkdir($this->conn,$dirToCreate);

         if(file_exists($this->dirToCreate))
         {
            return false;
         } else {
            echo "<p>Directory: $dirToCreate created</p>n";
         }
    }

To allow me to set file permissions I create another method, I pass the file/folder to modify and the permissions level desired. Call the function ftp_chmod with the connection reference, the permission level and the file/folder to change.

function ftpFolderPermission($folderChmod,$permission){

        $this->folderChmod = $folderChmod;
        $this->permission = $permission;

        if (ftp_chmod($this->conn, $this->permission, $this->folderChmod) !== false)
        {
            echo "<p>$folderChmod chmoded successfully to ".$this->permission."</p>n";
        }
    }

The most useful method is to copy files from the current server to the new server, pass the method the remote file and the local file path, then use the function ftp_put pass it the connection ref then the location where you want to upload the file to followed by the location of the local file.

function ftpCopyFile($remoteFile,$localFile){

        $this->remoteFile = $remoteFile;
        $this->localFile = $localFile;

        if (ftp_put($this->conn,$this->remoteFile,$this->localFile,FTP_ASCII))
        {
            echo "<p>successfully uploaded $localFile to $remoteFile</p>n";
        } else {
            echo "<p>There was a problem while uploading $remoteFile</p>n";
        }
    }

That's it the class is complete it's very simply, while it's not a fully fledge ftp client it is very useful to copying a single file or multiple files quickly.

Here's the full class:

<?php
class FTP {

    private $host;
    private $user;
    private $pass;
    private $conn;
    private $dirToCreate;
    private $folderChmod;
    private $permission;
    private $remoteFile;
    private $localFile;

    public function __construct($host,$user,$pass){

        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;

        $this->conn = ftp_connect($this->host) or die("Could not connect to $host");
        ftp_login($this->conn,$this->user,$this->pass);
        echo "connected ok ".$this->host;
    }

    function ftpClose()    {
        ftp_close($this->conn);
    }

    function ftpMkDir($dirToCreate){

         $this->dirToCreate = $dirToCreate;
         @ftp_mkdir($this->conn,$dirToCreate);

         if(file_exists($this->dirToCreate))
         {
            return false;
         } else {
            echo "<p>Directory: $dirToCreate created</p>n";
         }
    }

    function ftpFolderPermission($folderChmod,$permission){

        $this->folderChmod = $folderChmod;
        $this->permission = $permission;

        if (ftp_chmod($this->conn, $this->permission, $this->folderChmod) !== false)
        {
            echo "<p>$folderChmod chmoded successfully to ".$this->permission."</p>n";
        }
    }

    function ftpCopyFile($remoteFile,$localFile){
        $this->remoteFile = $remoteFile;
        $this->localFile = $localFile;

        if (ftp_put($this->conn,$this->remoteFile,$this->localFile,FTP_ASCII))
        {
            echo "<p>successfully uploaded $localFile to $remoteFile</p>n";
        } else {
            echo "<p>There was a problem while uploading $remoteFile</p>n";
        }
    }
}
?>

So what's next? well that's up to you but a good use of this class could be to do a database dump and save it to a file then using this class copy the database backup to another server, along with a cron job you'll then have a fully automated backup solution, Nice!

0 comments
Add a comment

Copyright © 2024 DC Blog - All rights reserved.