Linux教程網
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <?php
- /*
- php ftp類主要功能:
- 1.連接ftp並登陸;
- 2.創建目錄和刪除目錄;
- 3.上傳文件和刪除文件;
- */
-
-
-
- include 'config.php';
-
- class Net_FTP {
- var $ftp_server;
- var $ftp_user;
- var $ftp_pass;
- var $ftp_port;
- var $conn_id;
- function Net_FTP() {
- $this->ftp_server = server;
- $this->ftp_user = username;
- $this->ftp_pass = password;
- $this->ftp_port = port;
- // 建立連接
- $this->conn_id = ftp_connect($this->ftp_server, $this->ftp_port) or die("不能夠連接到 $this->ftp_server");
- // 嘗試登陸
- if (!ftp_login($this->conn_id, $this->ftp_user, $this->ftp_pass))
- {
- $this->message("連接失敗 $this->ftp_user");
- }
- else
- {
- $this->message("連接成功 $this->ftp_user ");
- }
- }
- //功能:創建新的目錄
- //$path默認為空目錄
- //創建成功返回true,否則返回false。
- function newdir($path = null)
- {
- if($this->ftp_is_dir($this->conn_id,$path)||@ftp_mkdir($this->conn_id,$path))
- return true;
- if(!$this->newdir(dirname($path)))
- return false;
- ftp_mkdir($this->conn_id,$path);
- return true;
- }
- //驗證是否為目錄
- //對$path進行驗證:如果是目錄返回true,否則返回false。
- function ftp_is_dir($path)
- {
- $original_directory = ftp_pwd($this->conn_id);
- if(@ftp_chdir($this->conn_id,$path))
- {
- ftp_chdir($this->conn_id,$original_directory);
- return true;
- }
- else
- return false;
- }
- //功能:上傳文件
- //$ftppath:存在ftp服務器位置;$localpath:本地文件位置;
- //上傳成功返回true,否則返回false。
- function uploadfile($ftppath = null, $localpath = null)
- {
- if(!emptyempty($ftppath) && !emptyempty($localpath))
- {
- $ret = ftp_nb_put($this->conn_id, $ftppath, $localpath, FTP_BINARY);
- while ($ret == FTP_MOREDATA)
- {
- $ret = ftp_nb_continue ($this->conn_id);
- }
- if ($ret != FTP_FINISHED)
- {
- $this->message( "上傳失敗");
- return false;
- }
- else
- {
- $this->message("上傳成功");
- return true;
- }
- }
- }
- //功能:刪除目錄
- //$dir:要刪除的目錄
- //刪除成功返回true,否則返回false。
- function deldir($dir = null)
- {
- if (ftp_rmdir($this->conn_id, $dir))
- {
- $this->message("刪除目錄成功");
- return true;
- }
- else
- {
- $this->message("刪除目錄失敗");
- return false;
- }
- }
- //功能:返回目錄
- //返回當前目錄名稱
- function redir()
- {
- return ftp_pwd($this->conn_id);
- }
- //功能:刪除文件
- //$path:文件路徑
- //刪除成功返回true,否則返回false。
- function delfile($path = null)
- {
- if(ftp_delete($this->conn_id, $path))
- {
- $this->message("刪除文件成功");
- return true;
- }
- else
- {
- $this->message("刪除文件失敗");
- return false;
- }
- }
- //功能:打印信息
- //$str:要打印的信息
- function message($str = null)
- {
- if(!emptyempty($str))
- {
- echo $str;
- }
- }
- //功能:關閉ftp連接
- function closeftp()
- {
- ftp_close($this->conn_id);
- }
-
- }
- /*
- 一下為示范;
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <?php
- include 'ftp.php';
- $conn=new Net_FTP();
- //$ftppath=$conn->redir();
- $ftppath='/test';
- $locpath="/home/liye/public_html/php_ftp/test";
- //$conn->uploadfile($ftppath,$locpath);
- //$conn->newdir('test/123/1233');
- //$conn->deldir('test/123/1233');
- //$conn->delfile($ftppath);
- $conn->delfile('ftp.php');
- ?>
-
-
- */
Copyright ©
Linux教程網 All Rights Reserved