FTP functions (PHP)

Source: Internet
Author: User
Tags ftp login

<? 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;
}

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.