PHP implementation from the FTP server download the file tree to the local computer program _php tips

Source: Internet
Author: User
Tags ereg
Copy Code code as follows:

/*
When you use Ftp_nlist (), the returned array value can be of two types: varies by server
A: Separate file names
B: Contains the file name of the directory.
If misappropriated, please note change here.
*/
<?php
function Download_file ($dir, $FC, $_file_)
{
$fn =ftp_nlist ($FC, "."); /Lists the file name (including subdirectories) of the directory, 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 is file and directory, culling.,.. The two directories
{
if (Ereg (' ^[a-za-z0-9_]+ ([a-za-z0-9-]*.*) (\.+) ', $FN [$i]))//is a file to download directly
{
if (Ftp_get ($FC, $fn [$i], $FN [$i],ftp_binary))
{
echo "<br/> download". GETCWD (). " /". $FN [$i]." Success <br/> ";
}
Else
{
echo "<br/> download". GETCWD (). " /". $FN [$i]." Failure <br/> ";
}
}//File Download complete
else//"is the directory, access the directory, and then read the file";
{
if (!file_exists ($FN [$i]))
mkdir ($fn [$i], 0700)//The directory does not exist on the local machine to create a
if (Ftp_chdir ($FC, $FN [$i])
ChDir ($fn [$i]);
echo "The current directory is:". GETCWD (). <br/> "//better see the current directory
Download_file ($fn [$i], $FC, $_file_);//recursively enter the directory to download the file
}
}//extract files, end of directory
}//for Loop End
Ftp_cdup ($FC);//ftp server returns upper Directory
ChDir (DirName ($_file_));
}//download--file () function ends
$_file_= "Local Machine absolute address";//For example C:/download, don't do this c:/download/
$hostname = "server name";
$loginname = "User name";
$password = "User password";
$FC =ftp_connect ($hostname, "a") or Die ("couldn ' t connect to $hostname");
$FC _rw=ftp_login ($FC, $loginname, $password);
Ftp_set_option ($FC, ftp_timeout_sec,100000);/Set timeout time
$dir = ""; ChDir ($_file_);//Enter Local machine absolute address directory
Download_file ($dir, $FC, $_file_);
Ftp_quit ($FC);
Programs that download the file tree from the server
?>

Hope to bring you help, thank you
Related Article

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.