- /**
- * Role: FTP operation class (copy, move, delete files/create directory)
- * Time: 2006/5/9
- * Author: Gladly with the wind
- * qq:276624915
- */
- Class Class_ftp
- {
- Public $off; Return operation status (Success/failure)
- public $conn _id; FTP connection
- /**
- * Method: FTP connection
- * @FTP_HOST--FTP host
- * @FTP_PORT--Port
- * @FTP_USER-User name
- * @FTP_PASS--Password
- */
- function __construct ($FTP _host, $FTP _port, $FTP _user, $FTP _pass)
- {
- $this->conn_id = @ftp_connect ($FTP _host, $FTP _port) or Die ("FTP Server Connection failed");
- @ftp_login ($this->conn_id, $FTP _user, $FTP _pass) or Die ("FTP server login failed");
- @ftp_pasv ($this->conn_id,1); Turn on passive simulation
- }
- /**
- * Method: Upload File
- * @path--Local path
- * @newpath--Upload path
- * @type-New If the target directory does not exist
- */
- function Up_file ($path, $newpath, $type =true)
- {
- if ($type) $this->dir_mkdirs ($newpath);
- $this->off = @ftp_put ($this->conn_id, $newpath, $path, ftp_binary);
- if (! $this->off) echo "File upload failed, please check the permissions and path is correct!" ";
- }
- /**
- * Method: Move File
- * @path-The original path
- * @newpath-New Path
- * @type-New If the target directory does not exist
- */
- function Move_file ($path, $newpath, $type =true)
- {
- if ($type) $this->dir_mkdirs ($newpath);
- $this->off = @ftp_rename ($this->conn_id, $path, $newpath);
- if (! $this->off) echo "File move failed, please check the permissions and the original path is correct!" ";
- }
- /**
- * Method: Copy File
- * Description: Due to FTP no copy command, this workaround is: Download and upload to the new path
- * @path-The original path
- * @newpath-New Path
- * @type-New If the target directory does not exist
- */
- function Copy_file ($path, $newpath, $type =true)
- {
- $downpath = "C:/tmp.dat";
- $this->off = @ftp_get ($this->conn_id, $downpath, $path, ftp_binary);//download
- if (! $this->off) echo "File copy failed, please check the permissions and the original path is correct!" ";
- $this->up_file ($downpath, $newpath, $type);
- }
- /**
- * Method: Delete File
- * @path--Path
- */
- function Del_file ($path)
- {
- $this->off = @ftp_delete ($this->conn_id, $path);
- if (! $this->off) echo "File deletion failed, please check the permissions and path is correct!" ";
- }
- /**
- * Method: Generate Directory
- * @path--Path
- */
- function Dir_mkdirs ($path)
- {
- $path _arr = explode ('/', $path); Fetch directory Array
- $file _name = Array_pop ($path _arr); Popup file name
- $path _div = count ($path _arr); Number of layers to be taken
- foreach ($path _arr as $val)//Create Directory
- {
- if (@ftp_chdir ($this->conn_id, $val) = = FALSE)
- {
- $tmp = @ftp_mkdir ($this->conn_id, $val);
- if ($tmp = = FALSE)
- {
- echo "Directory Creation failed, please check the permissions and path is correct!" ";
- Exit
- }
- @ftp_chdir ($this->conn_id, $val);
- }
- }
- for ($i =1; $i = $path _div; $i + +)//fallback to root
- {
- @ftp_cdup ($this->conn_id);
- }
- }
- /**
- * Method: Close FTP connection
- */
- function Close ()
- {
- @ftp_close ($this->conn_id);
- }
- }//class Class_ftp End
- /************************************** Test ***********************************
- $ftp = new Class_ftp (' 192.168.100.143 ', +, ' user ', ' pwd '); Open FTP Connection
- $ftp->up_file (' aa.txt ', ' a/b/c/cc.txt '); Uploading files
- $ftp->move_file (' a/b/c/cc.txt ', ' a/cc.txt '); Moving files
- $ftp->copy_file (' a/cc.txt ', ' a/b/dd.txt '); Copying files
- $ftp->del_file (' a/b/dd.txt '); deleting files
- $ftp->close (); Close FTP connection
- ******************************************************************************/
- ?>
Copy Code
|