[英]SFTP local file upload using PHP
I have the following script below where I try to mimic a file upload via FTP but have changed it for SFTP using phpseclib. 我在下面使用以下脚本尝试模拟通过FTP上传的文件,但已使用phpseclib将其更改为SFTP。
The script echoes out fine until the line: 脚本回显良好,直到该行为止:
$upload = $conn_id->put($paths.'/'.$name, $filep, NET_SFTP_LOCAL_FILE);
$ upload = $ conn_id-> put($ paths。'/'。$ name,$ filep,NET_SFTP_LOCAL_FILE); echo "upload == ".$upload."\\n";
回声“ upload ==”。$ upload。“ \\ n”;
where nothing happens or prints out. 没有任何反应或无法打印的地方。
here is the full script: 这是完整的脚本:
<?
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);
}
?>
Which library is NET/ssh2.php? 哪个库是NET / ssh2.php? Looks like http://phpseclib.sourceforge.net ?
看起来像http://phpseclib.sourceforge.net吗?
You're mixing their SSH2 and SFTP libraries. 您正在混合使用他们的SSH2和SFTP库。 Taken directly from their manual, the code you want is:
直接从他们的手册中获取的代码是:
<?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());
?>
So just change the $data in the "put" function to be the filename, and adding the mode as you've done in your code. 因此,只需将“ put”函数中的$ data更改为文件名,然后像在代码中一样添加模式即可。
An alternative would be the PECL functions provided by PHP. 一种替代方法是PHP提供的PECL函数。 These have sftp functions "built in".
这些具有“内置”的sftp函数。 Example code is http://www.php.net/manual/en/function.ssh2-sftp.php and if it still doesn't work, we'll be better placed to help debug as we can all access it.
示例代码为http://www.php.net/manual/en/function.ssh2-sftp.php ,如果仍然无法使用,我们将可以更好地帮助调试,因为我们都可以访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.