CSDN博客

img keenx

FTP函数(php)

发表于2004/7/16 16:34:00  889人阅读

<?php
//定义FTP登录服务器,用户名,密码
$ftp_server = '192.168.0.130';
$ftp_user_name = 'staff';
$ftp_user_pass = '888';
$server_file = 'listfile.txt';
$local_file = 'listfile2.txt';

//建立连接实例
$conn_id = ftp_connect($ftp_server);
//登录,返回BOOL值
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if ((!$conn_id) || (!$login_result)) {
       echo "登录服务器: $ftp_server 用户名: $ftp_user_name 失败";
       die;
   } else {
       echo "登录服务器: $ftp_server, 用户名: $ftp_user_name 成功";
   }

//下载FTP服务器上面的文件
if (ftp_get($conn_id, $server_file, $server_file, FTP_BINARY)) {
   echo "成功地下载了 $server_file/n";
} else {
   echo "下载失败/n";
}

//取得要下载的数组列表
$dfiles = downfiles($serverfile,$local_file);

//逐个下载数组列表清单中的文件
//每下载一个,就删除一个。
foreach($dfiles as $dkey){
 if(ftp_get($conn_id,$dkey,$dkey,FTP_BINARY)){
  echo "下载成功 $dkey <br>";
  if(ftp_delete($conn_id, $dkey)){
   echo "成功删除 $dkey/n <br>";
  }else{
   echo "删除失败 $dkey/n <br>";
  }
 }else{
  echo "下载失败 $dkey/n <br>";
 }
}


function getfile($filename){
 // get contents of a file into a string
 $handle = fopen ($filename, "r");
 $contents = fread ($handle, filesize ($filename)-2);
 fclose ($handle);
 $test = split("/r/n",$contents);
 return $test;
}

function downfiles($serverfile,$localfile){
 $files = getfile("listfile.txt");
 foreach($files as $key)
 {
  echo $key."<br>";
  $keysp = split(" ",$key);
  $localfiles = getfile("listfile2.txt");
  $had = False;
  foreach($localfiles as $key2){
   $key2sp = split(" ",$key2);
   if($keysp[1] == $key2sp[1]){
    echo "i have ".$key."<br>";
    $had = True;
    if(strtotime($keysp[0])>strtotime($key2sp[0])) $had = False;
    break;
   }
  }
  echo $had."<br>";
  if(!$had) $downfiles = $downfiles." ".$keysp[1];
 }
 echo $downfiles;
 $downfilesp = split(" ",$downfiles);
return $downfilesp;
}

?>

0 0

相关博文

我的热门文章

img
取 消
img