FTP ile Serverda Dosya veya Klasör Taşımak
Aşağıdaki uygulama verilen host, kullanıcı adı, şifre, kaynak klasör ve hedef klasör bilgilerine göre dosyaları ve klasörleri FTP ile taşımanıza yardımcı olur.
<?php
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;
}
?>
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;
}
?>
Yorumunuzu Ekleyin