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;
}

November 22, 2007. PHP.

6 Comments

  1. revu replied:

    please i want more extra details for this code

  2. shivam0101 replied:

    you have not given the usage

  3. Eug replied:

    clear, thx

  4. 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.

  5. 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.

  6. Naren replied:

    please i want more extra details for this code and usage of it

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Trackback URI

Follow

Get every new post delivered to your Inbox.