@tpojka prvo da ti se zahvalim za link na PHP klase i objekte i na standarde. Pomaže vrlo
Zamolio bih tebe (i one koji imaju iskustva s OOP) da mi nakratko proletiš kroz klasu koju sam danas napravio (prateći principe i standarde koliko sam uspio ) i kažeš svoje mišljenje o:
- da li je ispravno napisan? (OOP standardi, principi…)
- radi li se to na taj način? (da li bi ti napravio tako?)
- prolazi li ovakav code ili postoje greške koje sam napravio u njemu (sam code je ispravan i funkcionalan)
- na što moram pripaziti kod pisanja ovakvih klasa (primjerice, jesam li ga mogao kako skratiti koristeći principe oop-a)?
FtpUploadToServer.class.php:
<?php class FtpUploadToServer { const VERSION="1.0"; public $ftp_folder=""; public $ftp_file=""; public $createfolder=0; public $fileoverwrite=1; public $showmessages=1; public $timeout=60; public $passivemode=1; public $uploadmode=FTP_ASCII; // or FTP_BINARY function __construct($hostname, $username, $password,$port ){ $this->hostname=$hostname; $this->username=$username; $this->password=$password; $this->port=$port; } public function ftpUpload() { $this->showMessage("PHP FTP uploader Version ".self::VERSION); if ($this->connectToServer()) { $this->uploadFile(); $this->closeConnection(); } } private function connectToServer() { $this->ftpconn_id=@ftp_connect ($this->hostname , $this->port, $this->timeout); if(is_resource($this->ftpconn_id)) { if (@ftp_login ($this->ftpconn_id, $this->username, $this->password)){ @ftp_pasv($this->ftpconn_id,$this->passivemode); $this->showMessage("FTP connection established"); $this->showMessage("Logged in"); return true; } else { $this->showMessage("Login error: Please check your username and password for the host ".$this->hostname); } } else { $this->showMessage("Connection error: check hostname and port (current Host:".$this->hostname." Port:".$this->port.")"); } return false; } private function uploadFile() { if(@ftp_chdir($this->ftpconn_id,$this->ftp_folder)) { $remotefileexists=@ftp_size($this->ftpconn_id, $this->ftp_file); if ($remotefileexists<0 || ($remotefileexists>=0 && $this->fileoverwrite==1 )) { if(ftp_put($this->ftpconn_id,$this->ftp_file,$this->ftp_file, $this->uploadmode)) { $this->showMessage("File ".$this->ftp_file." successfuly uploaded"); return true; } else { $this->showMessage("Error! File ".$this->ftp_file." cannot be uploaded!"); } } else { $this->showMessage("File ".$this->ftp_file." exists! File not uploaded - fileoverwrite disabled!"); } } else { if ($this->createfolder==1) { if (@ftp_mkdir($this->ftpconn_id, $this->ftp_folder)) { $this->uploadFile(); } else { $this->showMessage("Cannot create folder at host ".$this->hostname); } } else { $this->showMessage($this->hostname." directory ".$this->ftp_folder." not exists!"); } } return false; } private function closeConnection() { ftp_close($this->ftpconn_id); $this->showMessage("FTP connection closed"); return true; } private function showMessage($message) { if ($this->showmessages) { echo $message."<br/>"; } } } ?>
Korištenje:
index.php:
<?php require("classes/FtpUploadToServer.class.php"); $ftp=new FtpUploadToServer("example.com","korisnickoime","lozinka","21"); $ftp->showmessages=1; $ftp->ftp_folder="testfolder"; $ftp->ftp_file="test.txt"; $ftp->createfolder=1; $ftp->fileoverwrite=1; $ftp->ftpUpload(); ?>
Pa eto molim, osim prozvanoga mladog gospona, ako tko drugi ima iskustva - molio bih pregled i komentar (da dobijem smjernice u radu s objektima).
Zauzvrat sam spreman platit pivu!
Hvala!