Php program for downloading the file tree from the ftp server to the Local Computer

Source: Internet
Author: User
Tags ereg

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.

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.