日期:2014-05-17  浏览次数:20528 次

php sftp上传下载
	<?php
		//php环境中必须有ssh
		$strServer = "ftp.***.com.cn";
		$strServerPort = "22";
		$strServerUsername = "***";
		$strServerPassword = "***";
		
		//connect to server
		$resConnection = ssh2_connect($strServer, $strServerPort);
		
		if(ssh2_auth_password($resConnection, $strServerUsername, $strServerPassword)){
			//init SFTP
			$resSFTP = ssh2_sftp($resConnection);
			
			//download a File
			//1
			$filename = 'c:/abc.jpg';
			$opts = array(
				'http'=>array(
				'method'=>"GET",
				'timeout'=>60,
				)
			);
			$context = stream_context_create($opts);
//			$strData = file_get_contents("ssh2.sftp://{$resSFTP}/dfr508/WUN/ikea-logo.jpg", false, $context);
//			file_put_contents($filename, $strData);
			
			//2 也可以用copy()
			if(!copy("ssh2.sftp://{$resSFTP}/dfr508/WUN/ikea-logo.jpg", $filename)) {
				echo 'download failed';
			}
			
			//upload a File
			//1
			//file_put_contents("ssh2.sftp://{$resSFTP}/dfr508/WUN/456.jpg", 'c:/123.jpg');
			//2
//			if(!copy("c:/abc.jpg", "ssh2.sftp://{$resSFTP}/dfr508/WUN/789.jpg")) {
//				echo 'upload failed';
//			}
		} else {
			echo "Unable to authenticate on server";
		}
	?>