<? PHP
// Define the FTP login server, user name, and password
$ Ftp_server = '192. 168.0.130 ';
$ Ftp_user_name = 'staff ';
$ Ftp_user_pass = '20140901 ';
$ Server_file = 'listfile.txt ';
$ Local_file = 'listfile2.txt ';
// Create a connection instance
$ Conn_id = ftp_connect ($ ftp_server );
// Log on and return the bool Value
$ Login_result = ftp_login ($ conn_id, $ ftp_user_name, $ ftp_user_pass );
If ((! $ Conn_id) | (! $ Login_result )){
Echo "Login Server: $ ftp_server Username: $ ftp_user_name failed ";
Die;
} Else {
Echo "Login Server: $ ftp_server, Username: $ ftp_user_name succeeded ";
}
// Download the file on the FTP server
If (ftp_get ($ conn_id, $ server_file, $ server_file, ftp_binary )){
Echo "$ server_file/N" is successfully downloaded ";
} Else {
Echo "Download failed/N ";
}
// Obtain the list of arrays to be downloaded
$ Dfiles = downfiles ($ serverfile, $ local_file );
// Download the files in the array list one by one
// Delete each downloaded file.
Foreach ($ dfiles as $ dkey ){
If (ftp_get ($ conn_id, $ dkey, $ dkey, ftp_binary )){
Echo "Download successful $ dkey <br> ";
If (ftp_delete ($ conn_id, $ dkey )){
Echo "$ dkey/N is successfully deleted <br> ";
} Else {
Echo "failed to delete $ dkey/n <br> ";
}
} Else {
Echo "Download failed $ dkey/n <br> ";
}
}
Function GetFile ($ filename ){
// Get contents of a file into a string
$ Handle = fopen ($ filename, "R ");
$ Contents = fread ($ handle, filesize ($ filename)-2 );
Fclose ($ handle );
$ Test = Split ("/R/N", $ contents );
Return $ test;
}
Function downfiles ($ serverfile, $ localfile ){
$ Files = GetFile ("listfile.txt ");
Foreach ($ files as $ key)
{
Echo $ key. "<br> ";
$ Keysp = Split ("", $ key );
$ Localfiles = GetFile ("listfile2.txt ");
$ Had = false;
Foreach ($ localfiles as $ key2 ){
$ Key2sp = Split ("", $ key2 );
If ($ keysp [1] ==$ key2sp [1]) {
Echo "I have". $ key. "<br> ";
$ Had = true;
If (strtotime ($ keysp [0])> strtotime ($ key2sp [0]) $ had = false;
Break;
}
}
Echo $ had. "<br> ";
If (! $ Had) $ downfiles = $ downfiles. "". $ keysp [1];
}
Echo $ downfiles;
$ Downfilesp = Split ("", $ downfiles );
Return $ downfilesp;
}
?>