Home php教程 php手册 php实现从ftp服务器上下载文件树到本地电脑的程序

php实现从ftp服务器上下载文件树到本地电脑的程序

Jun 13, 2016 pm 12:25 PM
ftp php superior download code copy accomplish document server local Tree use computer of program

复制代码 代码如下:


/*
用ftp_nlist()函授时,返回的数组值会有两种类型:因服务器不同而异
a:单独的文件名
b:包含目录的文件名。
如果挪用,请注意更改此处。
*/
function download_file($dir,$fc,$_FILE_)
{
$fn=ftp_nlist($fc,".");//列出该目录的文件名(含子目录),存储在数组中
$size=sizeof($fn);
$dir=($dir=="")?$dir:('/'.$dir);
$_FILE_=$_FILE_.$dir;
echo $_FILE_."
";
for($i=0;$i{
if(ereg('^[a-zA-Z0-9_]+',$fn[$i])) //提取是文件和目录,剔除.,..这两个目录
{
if(ereg('^[a-zA-Z0-9_]+([a-zA-Z0-9-]*.*)(\.+)',$fn[$i])) //是文件时直接下载
{
if(ftp_get($fc,$fn[$i],$fn[$i],FTP_BINARY))
{
echo "
下载".getcwd()."/".$fn[$i]."成功
";
}
else
{
echo "
下载".getcwd()."/".$fn[$i]."失败
";
}
}//文件下载结束
else //"是目录,进入目录,再读取文件";
{
if(!file_exists($fn[$i]))
mkdir($fn[$i], 0700);//本地机器上该目录不存在就创建一个
if(ftp_chdir($fc,$fn[$i]))
chdir($fn[$i]);
echo "当前的目录是:".getcwd()."
";// 更好的看清当前目录
download_file($fn[$i],$fc,$_FILE_);//递归进入该目录下载文件
}
}//提取文件,目录结束
}//for循环结束
ftp_cdup($fc);//ftp服务器返回上层目录
chdir(dirname($_FILE_));
}//download——file()函数结束
$_FILE_="本地机绝对地址";//例如c:/download,不要这样c:/download/
$hostname="服务器名";
$loginname="用户名";
$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);//设置超时时间
$dir="";chdir($_FILE_);//进入本地机绝对地址目录
download_file($dir,$fc,$_FILE_);
ftp_quit($fc);
//从服务器下载文件树的程序
?>


希望能给大家带去帮助,谢谢
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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How to fine-tune deepseek locally How to fine-tune deepseek locally Feb 19, 2025 pm 05:21 PM

How to fine-tune deepseek locally

deepseek image generation tutorial deepseek image generation tutorial Feb 19, 2025 pm 04:15 PM

deepseek image generation tutorial

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node?

binance official website URL Binance official website entrance latest genuine entrance binance official website URL Binance official website entrance latest genuine entrance Dec 16, 2024 pm 06:15 PM

binance official website URL Binance official website entrance latest genuine entrance

How to ask deepseek How to ask deepseek Feb 19, 2025 pm 04:39 PM

How to ask deepseek

How to download deepseek How to download deepseek Feb 19, 2025 pm 05:45 PM

How to download deepseek

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles