Home Backend Development PHP Tutorial php实现从ftp服务器上下载文件树到本地电脑的程序_php技巧

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

May 17, 2016 am 09:33 AM
ftp php server

复制代码 代码如下:

/*
用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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

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 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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

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

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles