FTP函數(php)

來源:互聯網
上載者:User

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

?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.