<?
//如果您想要將目前伺服器的資料夾階層移動或複製到另一個遠端伺服器。那麼這將會很有幫助,因為這將會瀏覽目前伺服器的目錄,同時也會將該檔案複製到遠端伺服器上的相同目錄中。
//此腳本會透過 FTP 將此目錄和子目錄中的所有檔案複製到另一個遠端伺服器
function rec_copy ($source_path, $destination_path, $con)
{
ftp_mkdir($con, $destination_path);
ftp_site($con, 'CHMOD 0777 '.$destination_path);
ftp_chdir($con,$destination_path);
if (is_dir($source_path))
{
chdir($source_path);
$handle=opendir('.');
while (($file = readdir($handle))!==false)
{
if (($file != ".") && ($file != ".."))
{
if (is_dir($file))
{
// 這裡我限制資料夾名稱 'propertyimages' 不被複製到遠端伺服器。 -- VK
if($file != "propertyimages")
{
rec_copy ($source_path."/".$file, $file, $con);
chdir($source_path);
ftp_cdup($con);
}
}
if (is_file($file))
{
$fp = fopen($file,"r");
// 這會將空格轉換為 '_',使其不會拋出錯誤。 -- VK
ftp_fput ($con, str_replace(" ", "_", $file), $fp,FTP_BINARY);
ftp_site($con, 'CHMOD 0755 '.str_replace(" ", "_", $file));
}
}
}
closedir($handle);
}
}
// 建立 FTP 連線 --VK
$con = ftp_connect("69.18.213.131",21);
$login_result = ftp_login($con,"username","password");
// 這是遠端伺服器的根路徑-- VK
$rootpath = "mainwebsite_html";
// 這是來源目錄的實體路徑。實際上您也可以使用相對路徑。-- VK
$sourcepath = realpath("../")."/resdesk";
// 此目錄名稱只會變更最上層的目錄,而不會變更內部目錄-- VK
$destination_dir_name = "resdesk_".$account_id."/";
rec_copy ($sourcepath, $destination_dir_name, $con);
if (function_exists("ftp_close"))
{
ftp_close($con);
}
?>