<?php # Step 6 // *** Define your host, username, and password // *** Include the class include('ftp_class.php'); // *** Create the FTP object $ftpObj = new FTPClient(); // *** Connect if ($ftpObj -> connect(FTP_HOST, FTP_USER, FTP_PASS)) { ## -------------------------------------------------------- # Step 7 $dir = '/photos'; // *** Make directory $ftpObj->makeDir($dir); ## -------------------------------------------------------- # Step 8 $fileFrom = 'zoe.jpg'; $fileTo = $dir . '/' . $fileFrom; // *** Upload local file to new directory on server $ftpObj -> uploadFile($fileFrom, $fileTo); ## -------------------------------------------------------- # Step 9 // *** Change to folder $ftpObj->changeDir($dir); // *** Get folder contents $contentsArray = $ftpObj->getDirListing(); // *** Output our array of folder contents ## -------------------------------------------------------- # Step 10 $fileFrom = 'zoe.jpg'; # The location on the server $fileTo = 'zoe-new.jpg'; # Local dir to save to // *** Download file $ftpObj->downloadFile($fileFrom, $fileTo); } ?>
<?php # Step 2 Class FTPClient { // *** Class variables # Step 3 private $connectionId; private $loginOk = false; public function __construct() { } ## -------------------------------------------------------- # Step 4 private function logMessage($message, $clear=true) { $this->messageArray[] = $message; } ## -------------------------------------------------------- # Step 4 public function getMessages() { return $this->messageArray; } ## -------------------------------------------------------- # Step 5 public function connect ($server, $ftpUser, $ftpPassword, $isPassive = true) { // *** Set up basic connection $this->connectionId = ftp_connect($server); // *** Login with username and password $loginResult = ftp_login($this->connectionId, $ftpUser, $ftpPassword); // *** Sets passive mode on/off (default off) ftp_pasv($this->connectionId, $isPassive); // *** Check connection if ((!$this->connectionId) || (!$loginResult)) { $this->logMessage('FTP connection has failed!'); $this->logMessage('Attempted to connect to ' . $server . ' for user ' . $ftpUser, true); return false; } else { $this->logMessage('Connected to ' . $server . ', for user ' . $ftpUser); $this->loginOk = true; return true; } } ## -------------------------------------------------------- # Step 7 public function makeDir($directory) { // *** If creating a directory is successful... if (ftp_mkdir($this->connectionId, $directory)) { $this->logMessage('Directory "' . $directory . '" created successfully'); return true; } else { // *** ...Else, FAIL. $this->logMessage('Failed creating directory "' . $directory . '"'); return false; } } ## -------------------------------------------------------- # Step 8 public function uploadFile ($fileFrom, $fileTo) { // *** Set the transfer mode $mode = FTP_ASCII; } else { $mode = FTP_BINARY; } // *** Upload the file $upload = ftp_put($this->connectionId, $fileTo, $fileFrom, $mode); // *** Check upload status if (!$upload) { $this->logMessage('FTP upload has failed!'); return false; } else { $this->logMessage('Uploaded "' . $fileFrom . '" as "' . $fileTo); return true; } } ## -------------------------------------------------------- # Step 9 public function changeDir($directory) { if (ftp_chdir($this->connectionId, $directory)) { $this->logMessage('Current directory is now: ' . ftp_pwd($this->connectionId)); return true; } else { $this->logMessage('Couldn\'t change directory'); return false; } } ## -------------------------------------------------------- # Step 9 public function getDirListing($directory = '.', $parameters = '-la') { // get contents of the current directory $contentsArray = ftp_nlist($this->connectionId, $parameters . ' ' . $directory); return $contentsArray; } ## -------------------------------------------------------- # Step 10 public function downloadFile ($fileFrom, $fileTo) { // *** Set the transfer mode $mode = FTP_ASCII; } else { $mode = FTP_BINARY; } // open some file to write to //$handle = fopen($fileTo, 'w'); // try to download $remote_file and save it to $handle if (ftp_get($this->connectionId, $fileTo, $fileFrom, $mode, 0)) { return true; $this->logMessage(' file "' . $fileTo . '" successfully downloaded'); } else { return false; $this->logMessage('There was an error downloading file "' . $fileFrom . '" to "' . $fileTo . '"'); } } ## -------------------------------------------------------- # Step 11 public function __deconstruct() { if ($this->connectionId) { ftp_close($this->connectionId); } } ## -------------------------------------------------------- } ?>
Wynik tego skryptu:
Warning: ftp_mkdir() [function.ftp-mkdir]: Can't create directory: File exists in C:\xampp\htdocs\ftp_class.php on line 65 Array ( [0] => Failed creating directory "/photos" ) Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\ftp_class.php on line 85 Array ( [0] => Uploaded "zoe.jpg" as "/photos/zoe.jpg ) Array ( ) Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\ftp_class.php on line 140