CodeExample

CodeExample:4dab1e4e1ba2d

From Joomla! Documentation

Example

$host = 'joomla.org';
$port = 21;
$options = null;
$user = 'randomUserName';
$pass = 'thisIsDefinatlyNotThePassword';

jimport('joomla.client.ftp');

/*
 * The JFTP::getInstance() method will connect us to the FTP server and automatically
 * log us in. We could do the same with the connect() and login() methods. Also check 
 * out JClientHelper::getCredentials('ftp');
 */

$ftp = JFTP::getInstance($host, $port, $options, $user, $pass);

if($ftp->isConnected()){
   //We are connected.
   
   //Let's print the details of the root directory
   echo '<pre>';
   print_r($ftp->listDetails());
   echo '</pre>';

   /* Assuming we only have one "Joomla" directory, this would look like this.
   Array
   (
    [0] => Array
        (
            [type] => 1
            [rights] => drwxr-xr-x
            [user] => randomUserName
            [group] => ftpusers
            [size] => 4096
            [date] => 03-15
            [time] => 2008
            [name] => Joomla
        )
   )
   */
   //Let's create a directory
   $ftp->mkdir('test');

   //Let's create a file in that new directory
   $ftp->create('test/fairyTale.txt');

   //Let's write the first line of our Fairy Tale into our newly created text file
   $ftp->write('test/fairyTale.txt', 'Once upon a time, there was a litte Mermaid, called Arielle');

   //Let's read the fairy tale out of the file, and echo it out.
   $fairyTale = '';
   $ftp->read('test/fairyTale.txt', $fairyTale);
   echo $fairyTale;

   //Let's think of a name for our fairyTale, and rename the file
   $ftp->rename('test/fairyTale.txt', 'test/Arielle.txt');

   //I think you got the idea =)
}

This example was originally contributed by User:Batch1211.


Chris Davenport 12:07, 17 April 2011 (CDT) Edit comment