
28-10-2005, 01:39
|
 |
|
|
חבר מתאריך: 09.04.02
הודעות: 8,000
|
|
|
מחלקת FTP לשימוש חופשי
בעקבות הפרסום של Bumpy, החלטתי לפרסם מחלקת FTP שכתבתי (זו מחלקה מצומצמת, השארתי לכם הרבה מקום לדמיון )
אתם מוזמנים להוסיף פונקציות משלכם או לשנות את הפונקציות הקיימות.
בלי יותר מדי דיבורים, הנה המחלקה:
קוד PHP:
class ftp { public $ftp_result; public $num_operations; public $num_failed_operations; protected $ftp_connection; protected $ftp_login; protected $host; private $password; private $username; /** * FTP Class by dorkiaa. * * @param string $host * @param string $username * @param string $password */ function ftp($host, $username, $password) { set_time_limit(0); $this->password = $password; $this->username = $username; $this->host = $host; $this->ftp_connection = ftp_connect($this->host); if (!$this->ftp_connection) { $this->log("There was an error while trying to connect {$this->host}."); $this->num_failed_operations++; exit; } else { $this->log("Connected seccesfuly to {$this->host}."); } $this->ftp_login = ftp_login($this->ftp_connection, $this->username, $this->password); if (!$this->ftp_login) { $this->log("There was an error while trying to login to {$this->host}."); $this->num_failed_operations++; exit; } else { $this->log("Login with user {$this->username}."); } } /** * Uploads a file to the FTP server. * * @param string $newfilename * @param string $file */ public function ftp_upload($newfilename, $file) { unset($this->ftp_result); $this->ftp_result = ftp_put($this->ftp_connection, $newfilename, $file, FTP_BINARY); $this->num_operations++; if (!$this->ftp_result) { $this->log("There was an error while trying to upload {$file} to {$this->host}."); $this->num_failed_operations++; exit; } else { $this->log("The file {$file} has been seccessfuly uploaded to {$this->host}."); } } /** * Downloads a file from the FTP server. * * @param string $newfilename * @param string $file */ public function ftp_download($newfilename, $file) { unset($this->ftp_result); $this->ftp_result = ftp_get($this->ftp_connection, $newfilename, $file, FTP_BINARY); $this->num_operations++; if (!$this->ftp_result) { $this->log("There was an error while trying to download {$file} to {$newfilename}."); $this->num_failed_operations++; exit; } else { $this->log("The file {$file} has been seccessfuly downloaded, and saved to <a href=\"{$newfilename}\">{$newfilename}."); } } /** * Lists the files in a directory. * * @param string $directory */ public function ftp_list($directory = '/') { unset($this->ftp_result); $this->ftp_result = ftp_rawlist($this->ftp_connection, $directory); $this->num_operations++; if (!$this->ftp_result) { $this->log("There was an error while trying to list the directory {$directory}."); $this->num_failed_operations++; exit; } else { foreach ($this->ftp_result as $key => $value) { echo strrchr($value, ' ')."<br />"; } } } /** * Logs the operations. * * @param string $msg */ protected function log($msg) { echo $msg."<br />\n"; } }
כמובן שגם אשמח להערות
נערך לאחרונה ע"י דור בתאריך 28-10-2005 בשעה 02:04.
|