CopyCode The 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 );
// Download the object tree from the server Program
?>
Hope to help you. Thank you.