Jump to content

[SOLVED] SSH2 Copy File


Asheeown

Recommended Posts

I need to copy a file from one remote server to another remote server with valid ssh2 connections that are already made.  Do I have to use fopen to create the file then pull the content inside the file I want to copy and write it into the new one?  Or is there an easier way to go about this?

Link to comment
https://forums.phpfreaks.com/topic/169044-solved-ssh2-copy-file/
Share on other sites

I#m guessing you also have to use receive...:

 

http://php.oregonstate.edu/manual/en/function.ssh2-scp-recv.php

 

If when trying to use this function you get "Warning: ssh2_scp_send(): Failure creating remote file," then try using

 

$data = file_get_contents("ssh2.sftp://$sftp/path/to/file");

Hey Mar, sorry I didn't post back last night but I got it working.

 

<?php
$BashProfileSource = file_get_contents("ssh2.sftp://".$Source['sftp']."".$h[1]."/.bash_profile");
$BashProfileStream = fopen("ssh2.sftp://".$Destination['sftp']."".$h[1]."/.bash_profile", 'w');
if($BashProfileDest = fwrite($BashProfileStream, $BashProfileSource)) echo "<font color=\"green\">User ".$h['0'].": Created bash profile file</font><br>";
fclose($BashProfileStream);
ssh2_exec($Destination['Connection'], "chmod +x ".$h[1]."/.bash_profile");
?>

 

It's copying the .bash files for a user migration in linux :)

 

Thanks for the help the file_get_contents method worked the best.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.