php ftp get 本地路径,PHP下载FTP目录到本地

该代码段实现了一个FTP目录下载功能,通过PHP将FTP服务器上的目录及其子目录递归下载到本地。首先检查FTP连接及本地目标目录,然后遍历FTP目录,对每个子目录进行递归下载,遇到文件则直接使用ftp_get函数下载。如果在过程中发生错误,返回false,否则返回true。
摘要由CSDN通过智能技术生成

/**

* @brief FTP下载目录,将目录下载到本地

* @param $strSrcDirectoryName FTP目录名

* @param $strDestDirectoryName 本地保存的目录地址

* @return boolean (true|false)

*/

function ftp_download_directory($resFtpHandler, $strSrcDirectoryName, $strDestDirectoryName = null)

{

if (is_null($resFtpHandler))

{

return false;

}

if (is_null($strDestDirectoryName) === false)

{

if (is_dir($strDestDirectoryName) === false)

{

$bolRes = mkdir($strDestDirectoryName, 0777, true);

}

chdir($strDestDirectoryName);

}

if ($strSrcDirectoryName != '.')

{

if (@ftp_chdir($resFtpHandler, $strSrcDirectoryName) === false)

{

//是文件,直接拷贝

$bolRes =    ftp_get($resFtpHandler, $strSrcDirectoryName, $strSrcDirectoryName, FTP_BINARY);

if ($bolRes === false)

{

return false;

}

return true;

}

if (is_null($strDestDirectoryName))

{

if (is_dir($strSrcDirectoryName) === false)

{

$bolRes = mkdir($strSrcDirectoryName, 0777, true);

}

chdir($strSrcDirectoryName);

}

}

$arrChildDirectory = ftp_nlist($resFtpHandler, '.');

/** 空目录 直接返回上层目录 */

if ($arrChildDirectory === false)

{

ftp_chdir($resFtpHandler, '..');

chdir('..');

return false;

}

foreach ($arrChildDirectory as $strChildDirectory)

{

if ($strChildDirectory == '.' || $strChildDirectory == '..')

{

continue;

}

//如果是目录, 进行递归

if (@ftp_chdir($resFtpHandler, $strChildDirectory) === true)

{

ftp_chdir($resFtpHandler, '..');

ftp_download_directory($resFtpHandler, $strChildDirectory);

}

else

{

//不是目录,是文件,直接下载

//echo "current directory ".getcwd()."\n";

//echo "ftp currecnt directory ".ftp_pwd($resFtpHandler)."\n";

$bolRes =    ftp_get($resFtpHandler, $strChildDirectory, $strChildDirectory, FTP_BINARY);

if ($bolRes === false)

{

return false;

}

}

}

ftp_chdir($resFtpHandler, '..');

chdir('..');

return true;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值