<?php
/**
* Read and upload a folder to ftp server
* Copyright (c) 2009, Xu Yulei
* @author Xu Yulei
* @license free
*/
class FtpUpload{
/**
* General function
* upload whole folder $aFolder
* from $resourcePath to $destinationPath
* @return
* @param $aFolder array folder structure get from readFolder() function
* @param $destinationPath string
* @param $resourcePath string
* @param $ftpStream Object
*/
public function uploadFolder($aFolder,$destinationPath,$resourcePath,$ftpStream){
foreach($aFolder as $key=>$value){
if(is_array ($value)){
// create folder $key
ftp_mkdir($ftpStream, $destinationPath.'/'.$key);
$this->uploadFolder($value,$destinationPath.'/'.$key,$resourcePath.'/'.$key,$ftpStream);
}
else{
// upload file
ftp_put($ftpStream , $destinationPath.'/'.$value,$resourcePath.'/'.$value,FTP_ASCII);
}
}
}
/**
* Get folder structure
* @return array $aFolder
* foreach $structure as $key => $value
* if key is a number $value = file name
* if key is a string $value = array repreasent a folder, $key = folder name
* @param $path string
*/
public function readFolder($path){
$dh = opendir($path);
$aFolder = array();
while (($file = readdir($dh)) !== false) {
if(!(preg_match('/^\.{1,2}$/',$file)||preg_match('/^\.svn[a-z,0-9]*$/',$file))){
if(is_dir($path.'/'.$file)){
$aFolder[$file] = $this->readFolder($path.'/'.$file);
}
else{
$aFolder[] = $file;
}
}
}
return $aFolder;
}
}
?>
<?php
// TEST set
$ftpStream = ftp_connect('192.168.0.115',21);
ftp_login($ftpStream,'docseo','comberry');
//echo ftp_pwd($ftpStream).'<br/>';
$resourcePath = $_SERVER['DOCUMENT_ROOT']."docseo/docseo_websiteManager/gen/websites/105";
$destinationPath = 'html/rick';
ftp_mkdir($ftpStream, $destinationPath);
$oUploader = new FolderUploader();
$aFolder = $oUploader->readFolder($resourcePath);
$oUploader->uploadFolder($aFolder,$destinationPath,$resourcePath,$ftpStream);
ftp_close($ftpStream);
?>