1

我在下面有以下脚本,我尝试通过 FTP 模拟文件上传,但使用 phpseclib 将其更改为 SFTP。

该脚本会很好地回显,直到该行:

$upload = $conn_id->put($paths.'/'.$name, $filep, NET_SFTP_LOCAL_FILE); echo "upload == ".$upload."\n";

什么都没有发生或打印出来的地方。

这是完整的脚本:

<?
include('Net/SSH2.php');

if(!isset($_POST["submit"])){?>

<form action="upload.php" method="POST" enctype="multipart/form-data">
<table align="center">
<tr>
<td align="right">
Server:
</td>
<td>
<input size="50" type="text" name="server" value="">
</td>
</tr>
<tr>
<td align="right">
Username:
</td>
<td>
<input size="50" type="text" name="user"  value="">
</td>
</tr>
<tr>
<td align="right">
Password:
</td>
<td>
<input size="50" type="text" name="password" value="" >
</td>
</tr>
<tr>
<td align="right">
Path on the server:
</td>
<td>
<input size="50" type="text" name="pathserver" >
</td>
</tr>
<tr>
<td align="right">
Select your file to upload:
</td>
<td>
<input name="userfile" type="file" size="50">
</td>
</tr>
</table>
<table align="center">
<tr>
<td align="center">
<input type="submit" name="submit" value="Upload image" />
</td>
</tr>

</table>
</form>
<?}
else 
{

set_time_limit(300);//for setting 

$paths=$_POST['pathserver'];

echo "paths == ".$paths."\n";

$filep=$_FILES['userfile']['tmp_name'];

echo "filep == ".$filep."\n";

$sftp_server=$_POST['server'];

echo "sftp_server == ".$sftp_server."\n";

$sftp_user_name=$_POST['user'];

echo "sftp_user_name == ".$sftp_user_name."\n";

$sftp_user_pass=$_POST['password'];

echo "sftp_user_pass == ".$sftp_user_pass."\n";

$name=$_FILES['userfile']['name'];

echo "name == ".$name."\n";



// set up a connection to ftp server
$conn_id = new Net_SSH2($sftp_server);

// login with username and password
$login_result = $conn_id->login($sftp_user_name, $sftp_user_pass);

// check connection and login result
if ((!$conn_id) || (!$login_result)) {
       echo "SFTP connection has encountered an error!";
       echo "Attempted to connect to $sftp_server for user $sftp_user_name....";
       exit;
   } else {
       echo "Connected to $sftp_server, for user $sftp_user_name".".....";
   }


echo "HERE "."\n";
// upload the file to the path specified


$upload = $conn_id->put($paths.'/'.$name, $filep, NET_SFTP_LOCAL_FILE);
echo "upload == ".$upload."\n";

// check the upload status
if (!$upload) {
       echo "SFTP upload has encountered an error!";
   } else {
       echo "Uploaded file with name $name to $sftp_server ";
   }

// close the FTP connection
ftp_close($conn_id);    

}
?>
4

1 回答 1

1

NET/ssh2.php 是哪个库?看起来像http://phpseclib.sourceforge.net

您正在混合使用他们的 SSH2 和 SFTP 库。直接取自他们的手册,您想要的代码是:

<?php
include('Net/SFTP.php');

$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}

echo $sftp->pwd() . "\r\n";
$sftp->put('filename.ext', 'hello, world!');
print_r($sftp->nlist());
?>

因此,只需将“put”函数中的 $data 更改为文件名,并按照您在代码中所做的那样添加模式。

一种替代方法是 PHP 提供的 PECL 函数。这些具有“内置”的 sftp 功能。示例代码是http://www.php.net/manual/en/function.ssh2-sftp.php如果它仍然不起作用,我们将更好地帮助调试,因为我们都可以访问它。

于 2012-06-19T10:59:45.160 回答