Skip to content

Instantly share code, notes, and snippets.

@gabrieledarrigo
Created October 31, 2012 09:03
Show Gist options
  • Save gabrieledarrigo/3985971 to your computer and use it in GitHub Desktop.
Save gabrieledarrigo/3985971 to your computer and use it in GitHub Desktop.
class for ftp moving file
<?php
class FileDispatcher {
private $ftpServer = '';
private $ftpUser = '';
private $ftpPassword = '';
private $connection;
private $localFile;
private $remoteFolder;
private $log = array();
private function connect() {
$this->connection = ftp_connect($this->ftpServer);
if (ftp_login($this->connection, $this->ftpUser, $this->ftpPassword)) {
$this->setLog('Login al server FTP effettuato con successo alle ' . date('d-m-Y H:i:s', time()));
} else {
$this->setLog('Errore nella connessione FTP alle ' . date('d-m-Y H:i:s', time()));
}
}
private function checkDir($dir) {
$origin = ftp_pwd($this->connection);
if (@ftp_chdir($this->connection, $dir)) {
ftp_chdir($this->connection, $origin);
return true;
}
return false;
}
public function setFile($localFile, $remoteFolder) {
$this->localFile = $localFile;
$this->remoteFolder = $remoteFolder;
$this->connect();
if (ftp_put($this->connection, $this->remoteFolder, $this->localFile, FTP_BINARY)) {
$this->setLog('File ' . $this->localFile . ' caricato correttamente sul server FTP nella cartella remota '
. $this->remoteFolder . ' alle ' . date('d-m-Y H:i:s', time()));
} else {
$this->setLog('File locale ' . $this->localFile . ' non è stato caricato correttamente alle ' . date('d-m-Y H:i:s', time()));
}
$this->closeConnection();
}
public function setDir($dir) {
$this->connect();
if (!$this->checkDir($dir)) {
if (ftp_mkdir($this->connection, $dir)) {
$this->setLog('Directory ' . $dir . 'creata correttamente alle ' . date('d-m-Y H:i:s', time()));
} else {
$this->setLog('Errore nella creazione della cartella ' . $dir . date('d-m-Y H:i:s', time()));
}
} else {
$this->setLog('Cartella ' . $dir . ' già presente sul server FTP' . date('d-m-Y H:i:s', time()));
}
$this->closeConnection();
}
private function closeConnection() {
ftp_close($this->connection);
}
private function setLog($message) {
array_push($this->log, array(
'status' => $message
));
$fp = fopen($_SERVER["DOCUMENT_ROOT"] . '/log/' . 'logfile.txt', 'a+');
fwrite($fp, '[' . date('d-m-Y H:i:s', time()) . ']' . $message . "\n\r");
fclose($fp);
}
public function getLog() {
header('Content-type: application/json; charset=utf-8');
echo json_encode(array('log' => $this->log));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment