Question

I have the following script below where I try to mimic a file upload via FTP but have changed it for SFTP using phpseclib.

The script echoes out fine until the line:

$upload = $conn_id->put($paths.'/'.$name, $filep, NET_SFTP_LOCAL_FILE); echo "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);    

}
?>
Was it helpful?

Solution

Which library is NET/ssh2.php? Looks like http://phpseclib.sourceforge.net?

You're mixing their SSH2 and SFTP libraries. 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.

An alternative would be the PECL functions provided by PHP. These have sftp functions "built in". 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top