/**
* @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;
}