Classe de gestion ftp

Description

Voilà une classe permettant la gestion d'un serveur FTP (envoyer, supprimer, télécharger des fichiers etc ...)
Je pense que la source est suffisamment commentée mais si vous avez des questions n'hésitez pas !

Source / Exemple :


<?php
                                      ###########                                
                                      #Class FTP#
                                      ###########
/*
########################################
#       Exemple d'utilisation :        #
#<?php                                 #
#require 'FTP.class.php';              #
#$ftp = new FTP($host,$user,$password);#
#try {                                 #
#$ftp->connect();                      #
#  // Utilisation des méthodes         #
# }                                    #
#catch (FTPException $e) {             #
#	echo $e;                           #
#}                                     #
#?>                                    #
########################################*/

									  
class FTP {
/*

  • FTP stream
*
  • @var resource
  • /
private $_stream; /*
  • FTP user
*
  • @var string
  • /
private $_user; /*
  • FTP password
*
  • @var string
  • /
private $pwd; /*
  • FTP host
*
  • @var string
  • /
private $_host; /*
  • FTP port
*
  • @var int
  • /
private $_port = 21; /*
  • FTP timeout
*
  • @var int
  • /
private $_timeout = 90; /*
  • FTP passive mode flag
*
  • @var bool
  • /
private $_passive = false; /*
  • SSL-FTP connection flag
*
  • @var bool
  • /
private $_ssl = false; /*
  • System type of FTP server
*
  • @var string
  • /
public $system_type; /**
  • Initialize connection params
*
  • @param string $host
  • @param string $user
  • @param string $password
  • @param int $port
  • @param int $timeout (seconds)
  • /
public function __construct($host = null, $user = null, $password = null, $port = 21, $timeout = 90) { $this->_host = $host; $this->_user = $user; $this->_pwd = $password; $this->_port = (int)$port; $this->_timeout = (int)$timeout; } // Auto-close connection public function __destruct(){ $this->close(); } /* Set connection params :
  • @param int $port
  • @param int $timeout
  • @param bool $passive
  • @param bool $ssl
  • /
public function setParams($port =21, $timeout=90, $passive=false, $ssl=false) { $this->_port = (int)$port; $this->_timeout = (int)$timeout; $this->_passive = (bool)$passive; $this->_ssl = $ssl; } /* Close FTP connection
  • /
public function close() { // check for valid FTP stream if($this->_stream) { ftp_close($this->_stream); $this->_stream = false; } } /* Change current folder @param string $directory
  • /
public function chdir($directory) { if(ftp_chdir($this->_stream, $directory)) { } else { throw new FTPException("Failed to change name of directory to $directory"); } } /* Set file permissions @param int $permission (ex: 0644) @param string $file
  • /
public function chmod($permission = 0, $file = null) { if(ftp_chmod($this->_stream, $permission, $file)) { } else { throw new FTPException("Failed to set file permissions for $file"); } } /* Connect to FTP server
  • /
public function connect() { if(!$this->_ssl) { if(!$this->_stream = ftp_connect($this->_host, $this->_port, $this->_timeout)) { throw new FTPException("Failed to connect to {$this->_host}"); } } elseif(function_exists("ftp_ssl_connect")) { if(!$this->_stream = ftp_ssl_connect($this->_host, $this->_port, $this->_timeout)) { throw new FTPException("Failed to connect to {$this->_host} (SSL connection)"); } } else { throw new FTPException("Failed to connect to {$this->_host} (invalid connection type)"); } if(ftp_login($this->_stream, $this->_user, $this->_pwd)) { ftp_pasv($this->_stream, (bool)$this->_passive); $this->system_type = ftp_systype($this->_stream); } else { throw new FTPException("Failed to connect to {$this->_host} (login failed)"); } } /* Delete a file on server @param string $file
  • /
public function delete ($file = null) { if(ftp_delete($this->_stream, $file)) { } else { throw new FTPException("Failed to delete file $file"); } } /**
  • Download file from server
*
  • @param string $remote_file
  • @param string $local_file
  • @param int $mode
  • /
public function get($remote_file, $local_file, $mode = FTP_ASCII) { if(ftp_get($this->_stream, $local_file, $remote_file, $mode)) { } else { throw new FTPException("Failed to get file $remote_file"); } } /* Get date of last change of a file @param string $file @return string
  • /
public function mdtm($file) { return ftp_mdtm($this->_stream, $file); } /* Create folder on server @param string $directory
  • /
public function mkdir($directory) { if(ftp_mkdir($this->_stream, $directory)) { } else { throw new FTPException("Failed to create directory $directory on this server."); } } /* Get list of file/folder of a folder @param string $directory @return array
  • /
public function nlist($directory) { $list = array(); if($list = ftp_nlist($this->_stream, $directory)) { return $list; } else { throw new FTPException("Failed to get the file list of $directory"); } } /* Send a file on server @param string $local_file @param string $remote_file
  • /
public function put($local_file, $remote_file) { if(ftp_put($this->_stream, $remote_file, $local_file, FTP_ASCII)) { } else { throw new FTPException("Failed to upload file $local_file"); } } /* Get current folder's name @return string
  • /
public function pwd() { return ftp_pwd($this->_stream); } /* Get a detailed list of file/folder of a folder @param string $directory @return array
  • /
public function rawlist($directory) { $list = array(); if($list = ftp_rawlist($this->_stream, $directory)) { return $list; } else { throw new FTPException("Failed to get the file list of $directory"); } } /* Rename a file @param string $oldname @param string $newname
  • /
public function rename($oldname, $newname) { if(ftp_rename($this->_stream, $oldname, $newname)) { } else { throw new FTPException("Failed to rename file $oldname"); } } /* Delete a folder @param string $sirectory
  • /
public function rmdir($directory) { if(ftp_rmdir($this->_stream, $directory)) { } else { throw new FTPException("Failed to delete directory $directory"); } } /* Get file's size @param string $file @return int
  • /
public function size($file) { return ftp_size($this->_stream, $file); } } ################################################################### #Class FTPException & Interface IException & Class CustomException# ################################################################### interface IException { /* Protected methods inherited from Exception class */ public function getMessage(); // Exception message public function getCode(); // User-defined Exception code public function getFile(); // Source filename public function getLine(); // Source line public function getTrace(); // An array of the backtrace() public function getTraceAsString(); // Formated string of trace /* Overrideable methods inherited from Exception class */ public function __toString(); // formated string for display public function __construct($message = null, $code = 0); } abstract class CustomException extends Exception implements IException { protected $message = 'Unknown exception'; // Exception message private $string; // Unknown protected $code = 0; // User-defined exception code protected $file; // Source filename of exception protected $line; // Source line of exception private $trace; // Unknown public function __construct($message = null, $code = 0) { if (!$message) { throw new $this('Unknown '. get_class($this)); } parent::__construct($message, $code); } public function __toString() { return 'Error with message : ' . get_class($this) . " '{$this->message}' in {$this->file}(line {$this->line})<br />" . "{$this->getTraceAsString()}"; } } class FTPException extends CustomException {} ?>

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.