Php connects to ftp to upload, download, and delete file instance code. In fact, php itself is powerful. a large part of it is because it integrates many powerful functions.
Php ftp transfers files to the server
The code is as follows:
// Start
$ Ret = ftp_nb_get ($ my_connection, "test", "README", FTP_BINARY,
Filesize ("test "));
// Or: $ ret = ftp_nb_get ($ my_connection, "test", "README ",
// FTP_BINARY, FTP_AUTORESUME );
While ($ ret = FTP_MOREDATA ){
// Other code can be inserted.
Echo ".";
// Continue transmission...
$ Ret = ftp_nb_continue ($ my_connection );
}
If ($ ret! = FTP_FINISHED ){
Echo "Download error ...";
Exit (1 );
}
?>
Delete files using php ftp
The code is as follows:
$ File = 'public _ html/old.txt ';
// Connect to the FTP server
$ Conn_id = ftp_connect ('www .jb51.net ');
// Verify the user name and password
$ Login_result = ftp_login ($ conn_id, $ ftp_user_name, $ ftp_user_pass );
// Delete a specified object
If (ftp_delete ($ conn_id, $ file )){
Echo "$ file deleted successfully n ";
} Else {
Echo "failed to delete $ file n ";
}
// Close the FTP connection
Ftp_close ($ conn_id );
?>
Php ftp file download
The code is as follows:
$ File = 'somefile.txt ';
// Connect to the FTP server
$ Conn_id = ftp_connect ($ ftp_server );
// Verify the username and password www. jb51. net
$ Login_result = ftp_login ($ conn_id, $ ftp_user_name, $ ftp_user_pass );
// Obtain the size of the specified object
$ Res = ftp_size ($ conn_id, $ file );
If ($ res! =-1 ){
Echo "$ file size: $ res bytes ";
} Else {
Echo "failed to get the remote file size ";
}
// Close the FTP connection
Ftp_close ($ conn_id );
?>