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 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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 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)

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How to jump from Word plug-in to browser for login authorization? How to jump from Word plug-in to browser for login authorization? Apr 01, 2025 pm 08:27 PM

How to achieve login authorization from within the application to outside the application? In some applications, we often encounter the need to jump from one application to another...

How to implement cross-application jump for Word plug-in login authorization? How to implement cross-application jump for Word plug-in login authorization? Apr 01, 2025 pm 11:27 PM

How to implement cross-application jump for Word plug-in login authorization? When using certain Word plugins, we often encounter this scenario: click on the login in the plugin...

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

How to implement hot reload in Debian How to implement hot reload in Debian Apr 02, 2025 am 07:54 AM

Experience the convenience of Flutter hot reloading on the Debian system, just follow the steps below: Install FlutterSDK: First, you need to install FlutterSDK on the Debian system. Visit Flutter official website to download the latest stable version of SDK and decompress to the specified directory (for example, ~/flutter). After that, add Flutter's bin directory to the system PATH environment variable. Edit the ~/.bashrc or ~/.profile file, add the following code: exportPATH="$PATH:~/flutter/bin" Save the file and execute source~/.bas

How to convert XML to PDF on your phone? How to convert XML to PDF on your phone? Apr 02, 2025 pm 10:18 PM

It is not easy to convert XML to PDF directly on your phone, but it can be achieved with the help of cloud services. It is recommended to use a lightweight mobile app to upload XML files and receive generated PDFs, and convert them with cloud APIs. Cloud APIs use serverless computing services, and choosing the right platform is crucial. Complexity, error handling, security, and optimization strategies need to be considered when handling XML parsing and PDF generation. The entire process requires the front-end app and the back-end API to work together, and it requires some understanding of a variety of technologies.

xml online formatting xml online formatting Apr 02, 2025 pm 10:06 PM

XML Online Format Tools automatically organizes messy XML code into easy-to-read and maintain formats. By parsing the syntax tree of XML and applying formatting rules, these tools optimize the structure of the code, enhancing its maintainability and teamwork efficiency.

See all articles