I'm using phpseclib - SFTP class and am trying to upload a file like so -
$sftp = new Net_SFTP('mydomain.com');
if (!$sftp->login('user', 'password')) {
exit('Login Failed');
}
$sftp->put('/some-dir/',$fileTempName);
The file however isn't being uploaded inside some-dir but is uploaded one directory before (to the starting directory, lets say it's root). This is driving me crazy, I think I've tried all combinations of some-dir/ or /some-dir or /some-dir/, but the file won't upload there.
I don't think your put is doing what you think it is doing. According to the docs, you need to do a Net_SFTP::chdir('/some-dir/') to switch to the directory you want to send file to, then put($remote_file, $data), where remote_file is the name of file, and $data is the actual file data.
Example Code:
<?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());
?>
Related
I'm having trouble uploading an image from an HTML form to SFTP server. I'm using phpseclib to achieve this.
<?php
include('Net/SFTP.php');
$uploaded_file = $_FILES["my_image"]["tmp_name"];
$sftp = new Net_SFTP('my_server', 'my_port');
if (!$sftp->login('my_username', 'my_pass')) {
exit('Login Failed');
}
$sftp->mkdir('/home/new_dir');
$sftp->put($uploaded_file,'/home/new_dir/'.$uploaded_file, ), NET_SFTP_LOCAL_FILE);
?>
The connection is there and I can create a directory successfully, therefore I assume that the problem is here:
$sftp->put($uploaded_file,'/home/new_dir/'.$uploaded_file, ), NET_SFTP_LOCAL_FILE);
According to the documentation the order of your arguments should be the other way round.
$sftp->put('/home/new_dir/'.$uploaded_file, $uploaded_file, NET_SFTP_LOCAL_FILE);
This is my code to send single file from local to SFTP server. Single file successfully send.
I want to send one folder from local to SFTP server , but i dont know how to send one folder. i need help
<?php
$src = 'xxxxxxx';
$filename = 'test.txt';
$dest = 'xxxxxxxx'.$filename;
// set up sftp ssh-sftp connection
$connection = ssh2_connect('xxxxxx', 22);
ssh2_auth_password($connection, 'username', 'password');
// Create SFTP session
$sftp = ssh2_sftp($connection);
$sftpStream = #fopen('ssh2.sftp://'.$sftp.$dest, 'w');
try {
if (!$sftpStream) {
throw new Exception("Could not open remote file: $dest");
}
$data_to_send = #file_get_contents($src);
if ($data_to_send === false) {
throw new Exception("Could not open local file: $src.");
}
if (#fwrite($sftpStream, $data_to_send) === false) {
throw new Exception("Could not send data from file: $src.");
} else {
//Upload was successful, post-upload actions go here...
}
fclose($sftpStream);
} catch (Exception $e) {
error_log('Exception: ' . $e->getMessage());
fclose($sftpStream);
}
?>
ssh2_scp_send in php looks like just support sending file.
maybe these ways work:
1.compress folder, send the compressed file
2.use ssh2 to mkdir, then send each file iteratively
3.try with system() function to excute cmd line order "scp -r folder_path user#host:target_path"
I have to monitor the upload and download speed of a Raspberry to an SFTP server.
Such I have to put this data after on a website, at first I thinked to the old library ssh2 of PHP but it didn't send nothing as demanded on the server.
Like Phpseclib exist and seems to be more soft, I have installed Phpseclib on the Raspberry PI by composer. I think that everything is normally installed as some tutorial on website but when I'm using get and put like in the attachment code, CLI command freezes with get.php and give me nothing on the screen and put.php is giving the same time for a file of 1Mo as 512Ko (60 seconds) :/
Do you know how solve this kind of problem ? I had wait 15 min front of the CLI but she seems freezed.
Thanks a lot for your help !
Put.php
`<?php
include '/home/pi/vendor/autoload.php';
$sftp = new \phpseclib\Net\SFTP('192.168.100.20');
$key = new \phpseclib\Crypt\RSA();
$key->loadkey(file_get_contents('/home/pi/.ssh/id_rsa.pub'));
$key->loadkey(file_get_contents( '/home/pi/.ssh/id_rsa'));
if (!$sftp->login('vdar', $key)) {
exit('Login Failed');
}
$start = microtime(true);
$sftp->put('test.txt', str_repeat('a',1024*512));
$elapsed = microtime(true)-$start;
echo "took $elapsed seconds";
?>`
Get.php
`<?php
include '/home/pi/vendor/autoload.php';
define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);
$sftp = new \phpseclib\Net\SFTP('192.168.100.20');
$key = new \phpseclib\Crypt\RSA();
$key->loadkey(file_get_contents('/home/pi/.ssh/id_rsa.pub'));
$key->loadkey(file_get_contents( '/home/pi/.ssh/id_rsa'));
if (!$sftp->login('vdar', $key)) {
exit('Login Failed');
}
echo $sftp->pwd() . "\r\n";
$filename='remoteTest.txt';
if($filename)
{
$local_file_path='localTest.txt';
$sftp->get($filename, $local_file_path);
}
else
{
echo "error download files";
}?>`
I could not copy the file with spaces in file name using ssh2_scp_recv() function.This is the filename testfile-03_23_15 11 02 AM.csv which actually stored in server.
my code is here
if ($file == "testfile-03_23_15 11 02 AM.csv"){
if(!ssh2_scp_recv($connection,$remoteDir .$file, $localDir . $file)){
echo "Could not download: ", $remoteDir, $file, "\n";
}
}
Please help me if you know.
Thanks.
With phpseclib:
<?php
include 'phpseclib/Net/SSH2.php';
include 'phpseclib/Net/SCP.php';
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('bad login');
}
$scp = new Net_SCP($ssh);
$scp->get('file name with spaces');
Try this one:
ssh2_scp_recv($connection,"\"".$remote_file_name."\"",$local_path."/".$remote_file_name);
Source: php.net
It says: Trying to get a remote file with spaces in its name?
Never forget to use quotes in the remote filename, but never in the local one.
I am using the PECL ssh2 module to output XML data to a sftp server. I have two entirely separate PHP scripts which gather different data and send the output to different file on the stfp server.
CUSTOMER EXPORT:
$conn = ssh2_connect(SFTP_SERVER, SFTP_PORT);
ssh2_auth_password($conn, SFTP_USER, SFTP_PWD);
$sftp = ssh2_sftp($conn);
$file = 'ssh2.sftp://' . $sftp . CUSTOMER_EXPORT_PATH . CUSTOMER_EXPORT_FILENAME;
$doc = new DOMDocument('1.0','UTF-8');
CustomerExportXML($doc);
if (file_exists($file)) {
unlink($file);
}
$bytes_saved = $doc->save($file);
PRODUCT EXPORT:
$conn = ssh2_connect(SFTP_SERVER, SFTP_PORT);
ssh2_auth_password($conn, SFTP_USER, SFTP_PWD);
$sftp = ssh2_sftp($conn);
$file = 'ssh2.sftp://' . $sftp . PRODUCT_EXPORT_PATH . PRODUCT_EXPORT_FILENAME;
$doc = new DOMDocument('1.0','UTF-8');
ProductExportXML($doc);
if (file_exists($file)) {
unlink($file);
}
$bytes_saved = $doc->save($file);
In each case the XxxExportXML($doc) function takes a couple of minutes to gather the relevant data and stuff it in to $doc.
Each script works as is and exports the correct data to the correct place.
The problem is when their execution overlaps only the last one executed actually writes to the sftp server. If I echo out the $file variable then in each case they both have the same resource ID ie ssh2.sftp://ResourceID#150/Customer/Customer.xml and ssh2.sftp://ResourceID#150/Product/Product.xml
So my question is why are these two processes interfering with each other and how do I fix it so they can both be run at the same time?
So they're two different scripts? That's... weird. Maybe try phpseclib, a pure PHP SFTP implementation, instead. eg.
<?php
include('Net/SFTP.php');
$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
exit('Login Failed');
}
// puts a three-byte file named filename.remote on the SFTP server
$sftp->put('filename.remote', 'xxx');