Copy codeThe Code is as follows:
/*
When ftp_nlist () is used for correspondence, two types of returned array values are available:
A: separate file names
B: The file name that contains the directory.
If you use this function, make sure to change it here.
*/
<? Php
Function download_file ($ dir, $ fc, $ _ FILE _)
{
$ Fn = ftp_nlist ($ fc, "."); // list the file names (including subdirectories) of the Directory, which are stored in the array.
$ Size = sizeof ($ fn );
$ Dir = ($ dir = "")? $ Dir :( '/'. $ dir );
$ _ FILE _ = $ _ FILE _. $ dir;
Echo $ _ FILE _. "<br/> ";
For ($ I = 0; $ I <$ size; $ I ++)
{
If (ereg ('^ [a-zA-Z0-9 _] +', $ fn [$ I]) // extract the files and directories, remove the two directories...
{
If (ereg ('^ [a-zA-Z0-9 _] + ([a-zA-Z0-9-] *. *)(\. +) ', $ fn [$ I]) // directly downloads an object
{
If (ftp_get ($ fc, $ fn [$ I], $ fn [$ I], FTP_BINARY ))
{
Echo "<br/> download". getcwd (). "/". $ fn [$ I]. "successful <br/> ";
}
Else
{
Echo "<br/> download". getcwd (). "/". $ fn [$ I]. "failed <br/> ";
}
} // File Download is complete
Else // "is the directory, enter the directory, and then read the file ";
{
If (! File_exists ($ fn [$ I])
Mkdir ($ fn [$ I], 0700); // create
If (ftp_chdir ($ fc, $ fn [$ I])
Chdir ($ fn [$ I]);
Echo "the current directory is:". getcwd (). "<br/>"; // better understand the current directory
Download_file ($ fn [$ I], $ fc, $ _ FILE _); // recursively enter this directory to download files.
}
} // Extract the file and the directory ends
} // The for loop ends.
Ftp_cdup ($ fc); // the ftp server returns the upper directory
Chdir (dirname ($ _ FILE _));
} // Download -- file () function ends
$ _ FILE _ = "absolute address of the Local Machine"; // For example, c:/download. Do not use c:/download/
$ Hostname = "server name ";
$ Loginname = "username ";
$ Password = "User password ";
$ Fc = ftp_connect ($ hostname, "21") or die ("Couldn't connect to $ hostname ");
$ Fc_rw = ftp_login ($ fc, $ loginname, $ password );
Ftp_set_option ($ fc, FTP_TIMEOUT_SEC, 100000); // set the timeout value.
$ Dir = ""; chdir ($ _ FILE _); // enter the absolute address directory of the Local Machine
Download_file ($ dir, $ fc, $ _ FILE _);
Ftp_quit ($ fc );
// Program for downloading the file tree from the server
?>
Hope to help you. Thank you.