2012-03-20 58 views
0
<?php 
    $localfile = 'test1.txt'; 
    $fp = fopen ($localfile, "r") or die('Cannot open textfile'); 
    $connection = ssh2_connect('sftp_server_path/folder',22); 
    ssh2_auth_password($connection, 'username', 'password'); 

    $sftp = ssh2_sftp($connection) or die ("Cannot connect to server"); 


    $ch = curl_init(); 
    curl_setopt($ch,  CURLOPT_URL,"sftp_server_path/folder/test1.txt"); 
    curl_setopt($ch, CURLOPT_USERPWD, "username:password"); 
    curl_setopt($ch, CURLOPT_UPLOAD, 1); 
    curl_setopt($ch, CURLOPT_INFILE, $fp); 
    if(curl_exec($ch)){ 
    echo 'File was successfully transferred using SFTP'; 
    }else{ 
    echo 'File was unable to be transferred using SFTP'; 
    } 

?> 

我有上面的代碼連接並傳輸到sftp服務器,我運行此代碼我得到消息「文件已成功傳輸使用SFTP」。但是當我在sftp服務器中看到文件夾結構時,我無法在sftp服務器上看到我的傳輸文件。我到底要做什麼?如何從sftp服務器訪問文件?

+0

這意味着我的文件夾權限?其777. – user1122910 2012-03-20 06:56:10

回答

1

嘗試以下操作:

$sftp = ssh2_connect('shell.example.com', 22); 
ssh2_auth_password($sftp, 'username', 'password'); 

ssh2_scp_send($sftp, '/local/filename', '/remote/filename', 0644); 

http://sg2.php.net/manual/en/function.ssh2-scp-send.php

+0

ssh2_scp_send($ connection,'test1.txt','foldername/test1.txt',777); 我使用了這段代碼。但瀏覽器正在加載n加載........... – user1122910 2012-03-20 07:20:16

+0

抱歉應該'sft2_scp_send($ sftp,'而不是$ connection – 2012-03-20 07:23:02

+0

Thanks Done .... :) – user1122910 2012-03-20 08:05:13

0

對於可移植性,我建議使用phpseclib, a pure PHP SFTP implementation。如果你想移動到另一個服務器,使用像PECL SSH2這樣深奧的非標準擴展,那麼更難做到這一點,而phpseclib使得它非常容易實現即插即用。