class FTPExtension

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/FileTransfer/FTPExtension.php \Drupal\Core\FileTransfer\FTPExtension
  2. 10 core/lib/Drupal/Core/FileTransfer/FTPExtension.php \Drupal\Core\FileTransfer\FTPExtension
  3. 11.x core/lib/Drupal/Core/FileTransfer/FTPExtension.php \Drupal\Core\FileTransfer\FTPExtension

Defines a file transfer class using the PHP FTP extension.

Hierarchy

Expanded class hierarchy of FTPExtension

File

core/lib/Drupal/Core/FileTransfer/FTPExtension.php, line 8

Namespace

Drupal\Core\FileTransfer
View source
class FTPExtension extends FTP implements ChmodInterface {
    
    /**
     * {@inheritdoc}
     */
    public function connect() {
        $this->connection = ftp_connect($this->hostname, $this->port);
        if (!$this->connection) {
            throw new FileTransferException("Cannot connect to FTP Server, check settings");
        }
        if (!ftp_login($this->connection, $this->username, $this->password)) {
            throw new FileTransferException("Cannot log in to FTP server. Check username and password");
        }
    }
    
    /**
     * {@inheritdoc}
     */
    protected function copyFileJailed($source, $destination) {
        if (!@ftp_put($this->connection, $destination, $source, FTP_BINARY)) {
            throw new FileTransferException("Cannot move @source to @destination", 0, [
                "@source" => $source,
                "@destination" => $destination,
            ]);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    protected function createDirectoryJailed($directory) {
        if (!ftp_mkdir($this->connection, $directory)) {
            throw new FileTransferException("Cannot create directory @directory", 0, [
                "@directory" => $directory,
            ]);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    protected function removeDirectoryJailed($directory) {
        $pwd = ftp_pwd($this->connection);
        if (!ftp_chdir($this->connection, $directory)) {
            throw new FileTransferException("Unable to change the current directory to @directory", 0, [
                '@directory' => $directory,
            ]);
        }
        $list = @ftp_nlist($this->connection, '.');
        if (!$list) {
            $list = [];
        }
        foreach ($list as $item) {
            if ($item == '.' || $item == '..') {
                continue;
            }
            if (@ftp_chdir($this->connection, $item)) {
                ftp_cdup($this->connection);
                $this->removeDirectory(ftp_pwd($this->connection) . '/' . $item);
            }
            else {
                $this->removeFile(ftp_pwd($this->connection) . '/' . $item);
            }
        }
        ftp_chdir($this->connection, $pwd);
        if (!ftp_rmdir($this->connection, $directory)) {
            throw new FileTransferException("Unable to remove the directory @directory", 0, [
                '@directory' => $directory,
            ]);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    protected function removeFileJailed($destination) {
        if (!ftp_delete($this->connection, $destination)) {
            throw new FileTransferException("Unable to remove the file @file", 0, [
                '@file' => $destination,
            ]);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function isDirectory($path) {
        $result = FALSE;
        $curr = ftp_pwd($this->connection);
        if (@ftp_chdir($this->connection, $path)) {
            $result = TRUE;
        }
        ftp_chdir($this->connection, $curr);
        return $result;
    }
    
    /**
     * {@inheritdoc}
     */
    public function isFile($path) {
        return ftp_size($this->connection, $path) != -1;
    }
    
    /**
     * {@inheritdoc}
     */
    public function chmodJailed($path, $mode, $recursive) {
        if (!ftp_chmod($this->connection, $mode, $path)) {
            throw new FileTransferException("Unable to set permissions on %file", 0, [
                '%file' => $path,
            ]);
        }
        if ($this->isDirectory($path) && $recursive) {
            $filelist = @ftp_nlist($this->connection, $path);
            if (!$filelist) {
                // empty directory - returns false
                return;
            }
            foreach ($filelist as $file) {
                $this->chmodJailed($file, $mode, $recursive);
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
FileTransfer::$chrootPath private property Path to connection chroot.
FileTransfer::$connectionHandle private property The instantiated connection object.
FileTransfer::$hostname protected property The hostname for this file transfer.
FileTransfer::$jail protected property Full path to directory where file-transfer is restricted to.
FileTransfer::$password protected property The password for this file transfer. 1
FileTransfer::$port protected property The port for this file transfer. 1
FileTransfer::$username protected property The username for this file transfer. 1
FileTransfer::checkPath final protected function Checks that the path is inside the jail and throws an exception if not.
FileTransfer::chmod final public function Changes the permissions of the specified $path (file or directory).
FileTransfer::copyDirectory final public function Copies a directory.
FileTransfer::copyDirectoryJailed protected function Copies a directory. 1
FileTransfer::copyFile final public function Copies a file.
FileTransfer::createDirectory final public function Creates a directory.
FileTransfer::findChroot public function Returns the chroot property for this connection.
FileTransfer::fixRemotePath final protected function Returns a modified path suitable for passing to the server.
FileTransfer::removeDirectory final public function Removes a directory.
FileTransfer::removeFile final public function Removes a file.
FileTransfer::sanitizePath public function Changes backslashes to slashes, also removes a trailing slash.
FileTransfer::setChroot public function Sets the chroot and changes the jail to match the correct path scheme.
FileTransfer::__get public function Implements the magic __get() method.
FileTransfer::__isset public function
FileTransfer::__set public function
FileTransfer::__unset public function
FTP::factory public static function Defines a factory method for this class. Overrides FileTransfer::factory
FTP::getSettingsForm public function Returns a form to collect connection settings credentials. Overrides FileTransfer::getSettingsForm
FTP::__construct public function Constructs a Drupal\Core\FileTransfer\FileTransfer object. Overrides FileTransfer::__construct
FTPExtension::chmodJailed public function Changes the permissions of the file / directory specified in $path. Overrides ChmodInterface::chmodJailed
FTPExtension::connect public function Connects to the server. Overrides FileTransfer::connect
FTPExtension::copyFileJailed protected function Copies a file. Overrides FileTransfer::copyFileJailed
FTPExtension::createDirectoryJailed protected function Creates a directory. Overrides FileTransfer::createDirectoryJailed
FTPExtension::isDirectory public function Checks if a particular path is a directory. Overrides FileTransfer::isDirectory
FTPExtension::isFile public function Checks if a particular path is a file (not a directory). Overrides FileTransfer::isFile
FTPExtension::removeDirectoryJailed protected function Removes a directory. Overrides FileTransfer::removeDirectoryJailed
FTPExtension::removeFileJailed protected function Removes a file. Overrides FileTransfer::removeFileJailed

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.