PHP program to download file tree from ftp server to local computer_PHP tutorial

WBOY
Release: 2016-07-21 15:48:20
Original
1171 people have browsed it

Copy code The code is as follows:

/*
When using ftp_nlist() correspondence, the returned array value will have two types : Varies from server to server
a: Individual file name
b: File name containing directory.
If misappropriated, please change this.
*/
function download_file($dir,$fc,$_FILE_)
{
$fn=ftp_nlist($fc,".");//column The file name of the directory (including subdirectories) is stored in the array
$size=sizeof($fn);
$dir=($dir=="")?$dir:('/' .$dir);
$_FILE_=$_FILE_.$dir;
echo $_FILE_."
";
for($i=0;$i<$size;$ i++)
{
if(ereg('^[a-zA-Z0-9_]+',$fn[$i])) //Extract files and directories, exclude...,... Directory
{
if(ereg('^[a-zA-Z0-9_]+([a-zA-Z0-9-]*.*)(.+)',$fn[$ i])) //Download directly if it is a file
{
if(ftp_get($fc,$fn[$i],$fn[$i],FTP_BINARY))
{
echo "
Download".getcwd()."/".$fn[$i]."Successful
";
}
else
{
echo "
Download".getcwd()."/".$fn[$i]."Failed
";
}
}//File download ends
else //"It is a directory, enter the directory, and then read the file";
{
if(!file_exists($fn[$i]))
mkdir($fn[$i], 0700);//If the directory does not exist on the local machine, create one
if(ftp_chdir($fc,$fn[$i]))
chdir($fn[$i]);
echo "The current directory is: ".getcwd()."
";// Better see the current directory
download_file($fn[$i],$fc,$_FILE_);/ /Recursively enter the directory to download files
}
}//Extract files, end of directory
}//End of for loop
ftp_cdup($fc);//ftp server returns to the 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,"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
$dir="";chdir($_FILE_);//Enter the local machine's absolute address directory
download_file($dir,$fc,$_FILE_);
ftp_quit($fc);
/ /Program to download file tree from server
?>

I hope it can be helpful to everyone, thank you

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319789.htmlTechArticleCopy the code as follows: /* When using ftp_nlist() correspondence, the returned array value will have two types: Varies from server to server a: separate file name b: file name containing directory. If you move...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template