You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

161 lines
4.5 KiB

<?php
/*
* Autor: Rafael Sales
* Email: msn:raphasales@hotmail.com
*
* Modifica: 17/09/2013
* Autor: Amarildo Pereira
* Email: amrlcba@terra.com.br
*/
class ftp {
private $ftp_server;
private $login_ftp;
private $pass_ftp;
private $ftp_dir;
private $local_dir;
private $id_ftp;
private $ftp_port = 21;
private $ftp_time = 600;
private $ftp_contents;
private $login_result;
private $copy;
/*
* array contem extensoes dos arquivos a serem copiados.
*/
private $extensoes;
private $use_exceptions;
public function __construct($ftp) {
$this->ftp_server = $ftp['ftp_server'];
$this->login_ftp = $ftp['login_ftp'];
$this->pass_ftp = $ftp['pass_ftp'];
$this->ftp_dir = $ftp['ftp_dir'];
$this->local_dir = $ftp['local_dir'];
$this->extensoes = $ftp['extensoes'];
$this->use_exceptions = isset($ftp['use_exceptions']) ? $ftp['use_exceptions'] : 1;
}
public function connect($pasMod = false) {
/*
* Tenta se conectar ao servidor.
*/
$this->id_ftp = ftp_connect($this->ftp_server, $this->ftp_port, $this->ftp_time);
if (!$this->id_ftp)
$this->Except("Não foi possivel se conectar ao servidor de ftp!");
/*
* Executa o login do usuario .
*/
$this->login_result = $this->setLogin();
if (!$this->id_ftp)
$this->Except("Não foi possivel autenticar o usuario no servidor de ftp!");
/*
* Ativa o modo de transmissão para passivo.
*/
ftp_pasv($this->id_ftp, $pasMod);
return $this->login_result;
}
private function setLogin() {
return ftp_login($this->id_ftp, $this->login_ftp, $this->pass_ftp);
}
public function copyFile($localFile, $remoteFile, $mode = FTP_BINARY) {
/*
* Verifica se existe uma conexao com o servidor.
*/
if (!$this->login_result) {
$this->Except("Servidor nao conectado!");
}
$result = ftp_chdir($this->id_ftp, $this->ftp_dir);
if (!$result)
$this->Except("Nao foi possivel acessar os diretorio de ftp!");
$result = ftp_get($this->id_ftp, $localFile, $remoteFile, $mode);
$msg = "Erro ao copiar arquivo!\nLocal: $localFile\nRemoto: $remoteFile";
if (!$result) {
$this->Except($msg);
} else {
GravaLogPorta("Arquivo remoto: $remoteFile copiado com sucesso!");
}
return $result;
}
private function listFiles() {
/*
* coloque o diretorio de origem dos arquivos
*/
$result = ftp_chdir($this->id_ftp, $this->ftp_dir);
if (!$result)
$this->Except("Nao foi possivel acessar os diretorio de ftp!");
$this->ftp_contents = ftp_nlist($this->id_ftp, "");
if ($this->ftp_contents === false) {
$this->Except("Nao foi possivel listar os arquivos ftp!");
}
}
public function getListFiles() {
$this->listFiles();
return $this->ftp_contents;
}
public function copyFiles() {
$this->listFiles();
if (($this->local_dir != "") && (!is_dir($this->local_dir))) {
/*
* CRIA O DIRETORIO LOCAL,CASO ELE NAO EXISTA
*/
mkdir($this->local_dir, 0777);
}
for ($i = 0; $i < count($this->ftp_contents); $i++) {
$extensao = strrchr($this->ftp_contents[$i], '.');
/*
* Verifica se a extensao esta na lista de importacao.
*/
if (!in_array($extensao, $this->extensoes)) {
continue;
}
$this->copy = ftp_nb_get($this->id_ftp, $this->local_dir . $this->ftp_contents[$i], $this->ftp_contents[$i], FTP_BINARY);
while ($this->copy == FTP_MOREDATA) {
$this->copy = ftp_nb_continue($this->id_ftp);
}
if ($this->copy == FTP_FINISHED) {
GravaLogPorta(sprintf("Arquvo: %s copiado com sucesso!", $this->ftp_contents[$i]));
}
if ($this->copy != FTP_FINISHED) {
$this->Except("Ocorreu um erro ao baixar o arquivo " . $this->ftp_contents[$i]);
} else {
continue;
}
}
}
private function Except($msg) {
if ($this->use_exceptions) {
throw new Exception(GetExcept($msg));
} else {
die($msg);
}
}
public function __destruct() {
ftp_close($this->id_ftp);
}
}
?>