php实现从ftp服务器上下载文件树到本地电脑的程序_php技巧
/*
用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);
//从服务器下载文件树的程序
?>
希望能给大家带去帮助,谢谢

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
