Move or Upload Folder Instead of File Using PHP FTP Functions
function moveFolder($_server, $_user_name, $_user_pass, $local_dir, $remote_dir) {
// set up basic connection
$_conn_id = ftp_connect($_server);
// login with username and password
$_login_result = ftp_login($_conn_id, $_user_name, $_user_pass);
// check connection
if ((!$_conn_id) || (!$_login_result)) {
$_error = “FTP connection has failed!”;
$_error .= “Attempted to connect to $_server for user $_user_name”;
$result = false;
} else {
$_error = “Connected to $_server, for user $_user_name”;
$result = true;
}
$conn_id = $_conn_id;
@ ftp_mkdir($conn_id, $remote_dir);
$handle = opendir($local_dir);
while (($file = readdir($handle)) !== false) {
if (($file != ‘.’) && ($file != ‘..’)) {
if (is_dir($local_dir . $file)) {
//recursive call
moveFolder($conn_id, $local_dir . $file . ‘/’, $remote_dir . $file . ‘/’);
} else
$f[] = $file;
}
}
closedir($handle);
if (count($f)) {
sort($f);
@ ftp_chdir($conn_id, $remote_dir);
foreach ($f as $files) {
$from = @ fopen(“$local_dir$files”, ‘r’);
$moveFolder = ftp_fput($conn_id, $files, $from, FTP_BINARY);
// check upload status
if (!$moveFolder) {
$this->_error = “FTP upload has failed! From: ” . $local_dir . ” To: ” . $remote_dir;
$result = false;
} else {
$this->_error = “Uploaded $local_dir to $remote_dir as $this->_server”;
$result = true;
}
}
}
return $result;
}



revu replied:
please i want more extra details for this code
October 17, 2008 at 11:00 am. Permalink.
shivam0101 replied:
you have not given the usage
October 30, 2008 at 3:07 pm. Permalink.
Eug replied:
clear, thx
February 25, 2009 at 8:22 pm. Permalink.
Kürşad Çakır replied:
Great code. I really worry how much details and usage could be given after these codes. Details and usage are so obvios. Don’t be selfish. try some yourself.
Thanks again for the codes.
March 31, 2009 at 9:13 am. Permalink.
Rob replied:
It won’t work as opendir doesn’t support local file access, it’s only for access to files on the web server, so cannot be used for uploading! Unless I’m seriously missing something.
July 7, 2009 at 7:42 pm. Permalink.
Naren replied:
please i want more extra details for this code and usage of it
November 16, 2010 at 9:49 am. Permalink.