PHPFTP study notes
Ftp_alloc () allocates space for the files to be uploaded to the FTP server.
Ftp_cdup () changes the current directory to the parent directory on the FTP server.
Ftp_chdir () changes the current directory on the FTP server.
Ftp_chmod () sets the permissions on the file through FTP.
Ftp_close () to close the FTP connection.
Ftp_connect () open the FTP connection.
Ftp_delete () deletes files on the FTP server.
Ftp_exec () executes a program/command on FTP.
Ftp_fget () downloads a file from the FTP server and saves it to a local open file.
Ftp_fput () uploads an opened file and saves it as a file on the FTP server.
Ftp_get_option () returns various options for the current FTP connection.
Ftp_get () downloads files from the FTP server.
Ftp_login () log on to the FTP server.
Ftp_mdtm () returns the last modification time of the specified object.
Ftp_mkdir () creates a new directory on the FTP server.
Ftp_nb_continue () continuously obtains/sends the file (non-blocking ).
Ftp_nb_fget ()
Download the file from the FTP server and save it to a locally opened File (non-blocking)
Ftp_nb_fput ()
Upload an opened file and save it as a non-blocking file on the FTP server ).
Ftp_nb_get () downloads files from the FTP server (non-blocking ).
Ftp_nb_put () uploads the file to the server (non-blocking ).
Ftp_nlist () returns the list of objects in the specified directory.
Ftp_pasv () returns whether the current FTP passive mode is enabled.
Ftp_put () uploads the file to the server.
Ftp_pwd () returns the name of the current directory.
The alias of ftp_quit () ftp_close.
Ftp_raw () sends a raw command to the FTP server.
Ftp_rawlist () returns the detailed list of objects in the specified directory.
Ftp_rename () rename the file or directory on the FTP server.
Ftp_rmdir () deletes the directory on the FTP server.
Ftp_set_option () sets various FTP runtime options.
Ftp_site () sends the SITE command to the server.
Ftp_size () returns the size of the specified file.
Ftp_ssl_connect () opens a secure SSL-FTP connection.
Ftp_0000ype () returns the system type identifier of the remote FTP server.
Class FTPUtil {
Public $ off; // return the operation status (successful/failed)
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 logon failed ');
@ Ftp_pasv ($ this-> conn_id, 1); // enable passive simulation
}
/**
* Method: upload a 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. check whether the permission and path are correct! ';
}
}
/**
* Method: move a file
* @ Path -- 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 "an error occurred while moving the file. check whether the permission and original path are correct! ";
}
}
/**
* Method: copy an object
* Note: Because FTP does not have a copy command, the work und of this method is to download the file and upload it to a new path.
* @ Path -- 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 "an error occurred while copying the file. check whether the permission and original path are correct! ";
}
$ This-> up_file ($ downPath, $ newPath, $ type );
}
/**
* Method: delete an object.
* @ Path -- path
*/
Function del_file ($ path ){
$ This-> off = @ ftp_delete ($ this-> conn_id, $ path );
If (! $ This-> off ){
Echo "an error occurred while deleting the file. check whether the permission and path are correct! ";
}
}
/**
* Method: generate a directory
* @ Path -- path
*/
Function dir_mkdirs ($ path ){
$ Path_arr = explode ('/', $ path); // Retrieve the directory array
$ File_name = array_pop ($ path_arr); // the file name is displayed.
$ Path_p = count ($ path_arr); // number of layers
Foreach ($ path_arr as $ val) {// create a 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 whether the permission and path are correct! ";
Exit;
}
@ Ftp_chdir ($ this-> conn_id, $ val );
}
}
For ($ I = 1; $ I <= $ path_p; $ I ++) {// roll back to the root
@ Ftp_cdup ($ this-> conn_id );
}
}
/**
* Method: disable the FTP connection.
*/
Function close (){
@ Ftp_close ($ this-> conn_id );
}