从ftp服务器新版本文件的脚本

<!DOCTYPE html>  
<html>  
	<head>  
		<title>Mirror update</title>  
		<meta charset="utf-8">  
	</head>  
	<body>  
		<p>从ftp服务器下载新版本文件</p>  
		<?php  
			$host = '';
			$user = '';
			$password = '';
			$remotefile = '';
			$localfile = '';

			//1. 连接主机
			$conn = ftp_connect($host);
			if(!$conn)
			{
				echo '不能连接ftp服务器';
				exit;
			}

			//2. 登录
			$result = @ftp_login($conn, $user, $password);
			if(!$result)
			{
				echo '登录失败';
				exit;
			}

			//3. 检查远程文件是否已经更新
			if(file_exists($localfile))
			{
				$localtime = filemtime($localfile);
			}
			else
			{
				$localtime = 0;
			}
			$remotetime = ftp_mdtm($conn, $remotefile);
			if(!($remotetime >= 0))
			{
				echo '不能获取ftp文件的更新时间';
				$remotetime = $localtime + 1;
			}
			
			if(!($remotetime > $localtime))
			{
				echo '本地文件已经是最新的了';
				exit;
			}
			//4. 下载文件
			$fp = fopen($localfile, 'w');
			if(!$success = ftp_fget($conn, $fp, $remotefile, FTP_ASCII))
			{
				echo '不能下载文件';
				ftp_quit($conn);
				exit;
			}

			fclose($fp);
			echo '文件下载成功';
			//5. 关闭连接
			ftp_quit($conn);

		?>  
	</body>  
</html> 

猜你喜欢

转载自blog.csdn.net/liu2446426696/article/details/59625638
今日推荐