I've been busting my brain trying to get something to work, but it's been a long day and I'm at the same place as when I started
does anyone have a function that works? basically, I want to call it with the conn_id, the remote path, and the local path, and have it fetch me all the files
any help appreciated
ftp_get a directory
Moderator: General Moderators
Re: ftp_get a directory
How are you trying it now?
If this returns the list you want put the ftp_get($value,$savedlocalvalue) instead of the echo.
Code: Select all
$connect = ftp_connect("ftp.hostname.com");
$result = ftp_login($connect, "username", "password");
$a = ftp_nlist($connect, ".");// put your path where the . is
foreach($a as $value){
echo $value, "<BR>";
}
Re: ftp_get a directory
Eric! wrote:How are you trying it now?
If this returns the list you want put the ftp_get($value,$savedlocalvalue) instead of the echo.Code: Select all
$connect = ftp_connect("ftp.hostname.com"); $result = ftp_login($connect, "username", "password"); $a = ftp_nlist($connect, ".");// put your path where the . is foreach($a as $value){ echo $value, "<BR>"; }
I've tried a few functions, the latest one is this:
looks pretty simular to yours...the thing that gets me is I need it to crawl the directories and sub directories and keep going to get everything
Code: Select all
function backupLiveSite(){
$ftp_server = $this->ftp_live_server;
$ftp_username = $this->ftp_live_username;
$ftp_password = $this->ftp_live_password;
$backup_dir_on_cms = G_FTP_CMS_BACKUP_DIR;
$external_live_dir = "/";
if ($conn_id = ftp_connect($ftp_server,21)) {
// Yes - Login successful?
if (ftp_login($conn_id, $ftp_username, $ftp_password)) {
$this->transferFiles($conn_id, $external_live_dir, $backup_dir_on_cms);
} else {
return false;
}
ftp_close($conn_id);
}
}
function transferFiles($conn_id, $dirRemote, $dirLocal){
// get contents of the current directory
$contents = ftp_nlist($conn_id, $dirRemote);
foreach($contents as $file){
$file2 = "$dirRemote/$file";
$file3 = "$dirLocal/$file";
echo "file2:".$file2;
echo "<br>file3:".$file3;
if ( (ftp_size($conn_id, $file2) == -1) && !( ($file == ".") || ($file == "..") ) ){ //Directory
echo "<b>$file</b><br>";
mkdir($file3); //Make Folder
$this->transferFiles($conn_id, $file2,$file3);
}
elseif (ftp_size($conn_id, $file2) > 0) {
echo "<i>$file</i>";
//If File Exists Skip.
if (file_exists($file3)){
if (ftp_size($conn_id,$file2) == filesize($file3))
echo "- skipped <br>";
else {
ftp_get($conn_id,$file3,$file2,FTP_BINARY,filesize($file3)/9);
echo "- completed <br>";
}
}
else {
ftp_get($conn_id,$file3,$file2,FTP_BINARY);
echo "<br>";
}
}
}