Home > Backend Development > PHP Tutorial > PHP implements automatic ftp update of files

PHP implements automatic ftp update of files

WBOY
Release: 2016-07-25 09:00:29
Original
1329 people have browsed it
PHP implements the code to automatically update uploaded files using ftp. Friends in need can refer to it.

Full code.

<html>
<head>
<body>
<h1>Mirror update</h1>
<?php
/**
 * php ftp自动更新上传文件
 * site bbs.it-home.org
*/
//变量设置
$host='192.168.11.12';
$user='ftp_user';
$password='ftp_pwd';
$remotefile='/pub/files/test.rar';
$localfile='/tmp/files.rar';
//connect to host
$conn=ftp_connect($host);
if(!$conn)
{
 echo 'Error: 无法连接ftp服务器<br/>';
 exit;
}
echo "Connected to $host.<br/>";
@ $result=ftp_login($conn,$user,$password);
  if(!$result)
  {
   echo "Error: 用户 $user 登录失败。<br/>";
   ftp_quit($conn);
   exit;
  }
  echo "login as $user<br/>";
  //check file times to see if an update is required
  echo 'Checking file time....';
  if(file_exists($localfile))
  {
   $localtime=filetime($localfile);
   echo 'Local file last updated';
   echo date('G:i j-M-Y',$localtime);
   echo '<br/>';
  }
  else
     $localtime=0;
    
  $remotetime=ftp_mdtm($conn,$remotefile);// 获取远程文件的修改时间函数
     if (!($remotetime>=0))
     {
      echo 'Can/'t access remote file time.<br/>';
      $remotetime=$localtime+1; //make sure of an update
      
     }
     else
     {
      echo 'Remote file last updated';
      echo date('G:i j-M-Y',$remotetime);
      echo '<br/>';
     }
     if(!($remotetime>$localtime))
     {
      echo 'Local copy is up to date.<br />';
        exit;
     }
     //download file
     echo 'Getting file from server....<br />';
     $fp=fopen($localfile,'w');
     if(!$success=ftp_fget($conn,$fp,$remotefile));
     {
      echo 'Error:Could not download file';
      ftp_quit($conn);
      exit;
     }
     fclose($fp);
     echo "File download successfully";
     //close connection to host
     ftp_quit($conn);

//fget()函数的用法:    
//ftp_get($conn,$localfile,$remotefile);
?>
</body>
</head>
</html>
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template